2

I need access to very precise timing in a .NET application.

I need microsecond precision.

Is there an easy way to do this in .NET?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Brian Webster
  • 30,033
  • 48
  • 152
  • 225
  • This question seems to be a (worse) duplicate of http://stackoverflow.com/questions/307582/how-frequent-is-datetime-now-updated-or-is-there-a-more-precise-api-to-get-the, but I like the answer better. – Abacus Dec 19 '13 at 21:04

6 Answers6

6

System.Diagnostics.Stopwatch is the right class for this degree of granularity in your timing, but make sure you use your Stopwatch object's Elapsed.Ticks property instead of its ElapsedTicks property, as they are two different things.

  • ElapsedTicks (without the dot) refers to the number of ticks of the Stopwatch, and thus needs to be used in concert with Stopwatch.Frequency (which is not the same on all machines) to calculate the elapsed time.
  • Elapsed.Ticks (with the dot) gives the number of Timespan ticks, and is not dependent on the Stopwatch frequency.

This is probably one of the subtlest potential errors in .Net.

Also, beware that Stopwatch is highly granular, but it isn't necessarily all that accurate (depending on your definition of the term). The elapsed time returned by Stopwatch will tend to drift away from the system time.

On my laptop, Stopwatch runs almost 5 seconds ahead over 24 hours, and this effect is even worse on some other machines.

If you're timing short durations, of course, this effect should not matter much at all.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
  • Note that Stopwatch may be inaccurate on a laptop due to power saving measures on the CPU, however a modern Intel i7 or newer wouldn't suffer from this problem. In other words, if you're doing timing on a work PC, things will be much more accurate. – Contango Feb 14 '12 at 10:22
4

The Stopwatch class is the best precision that you can get, it uses the high frequency timer.
The frequency of the stopwatch depends upon the frequency of the CPU, but its usually in the millions times per second range.

If the CPU doesn't provide high frequency timer, then the class will fallback to system timer which is in the range 100-50 times per second.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Shay Erlichmen
  • 31,691
  • 7
  • 68
  • 87
3

Use the Stopwatch class, should do the trick.

Andy
  • 5,188
  • 10
  • 42
  • 59
2

The Stopwatch class is your best option due to it's accuracy:

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx

Example:

https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6167361.html

Community
  • 1
  • 1
Keith Adler
  • 20,880
  • 28
  • 119
  • 189
1

Besides System.Diagnostics.StopWatch also check this link: The Multimedia Timer for the .NET Framework. HTH

Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
0

I have found a method which works slightly more precisely then StopWatch, PerformanceCounters and every other implementation I come across and does not require Platform Invoke or otherwise.

See https://stackoverflow.com/questions/15725711/obtaining-microsecond-precision-using-net-without-platform-invoke

Let me know if you find otherwise!

Community
  • 1
  • 1
Jay
  • 3,276
  • 1
  • 28
  • 38