Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should definitely return ElapsedMilliseconds instead of ElapsedTicks. The value returned by ElapsedTicks is dependent upon the Stopwatch frequency, which can be different on different systems. It will not necessarily correspond to the Ticks property of a Timespan or DateTime object.</p> <p>See <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.elapsedticks.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.elapsedticks.aspx</a>.</p> <p>If you <em>do</em> want the extra resolution of Ticks, you should return <code>watch.Elapsed.Ticks</code> (i.e. Timestamp.Ticks) instead of <code>watch.ElapsedTicks</code> (this might be one of the subtlest <em>potential</em> errors in .Net). From MSDN:</p> <blockquote> <p>Stopwatch ticks are different from DateTime.Ticks. Each tick in the DateTime.Ticks value represents one 100-nanosecond interval. Each tick in the ElapsedTicks value represents the time interval equal to 1 second divided by the Frequency.</p> </blockquote> <p>Other than that, I guess your code is fine, although I think you'd be including some of the method-calling overhead in your measurements, which might be significant if the methods themselves take very little time to execute. Also, you probably would want to exclude the first call to the method from your calculated average, but I'm not sure how you'd do that in your class.</p> <p>One last point, which would probably not be relevant to most uses of this class: Stopwatch runs a bit fast compared to the system time. On my computer, it gets about 5 seconds (that's <em>seconds</em>, not milliseconds) ahead after 24 hours, and on other machines this drift can be even larger. So it's a little misleading to say it's highly <em>accurate</em>, when it's actually just highly <em>granular</em>. For timing short-duration methods, this obviously wouldn't be a significant problem.</p> <p>And one more last point, which certainly <em>is</em> relevant: I've often noticed while benchmarking that I'll get a bunch of running times that are all clustered within a narrow range of values (e.g. 80, 80, 79, 82 etc.), but occasionally something else will happen in Windows (like opening another program or my anti-virus kicks on or something) and I'll get a value wildly out of whack with the others (e.g. 80, 80, 79, 271, 80 etc.). I think a simple solution to this outlier problem is to use the <em>median</em> of your measurements instead of the <em>mean</em>. I don't know if Linq supports this automatically or not.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload