10

I made a small test application in C# that sets DateTime.Now and starts a StopWatch. Every ten seconds I print _stopwatch.Elapsed.TotalMilliseconds and (DateTime.Now - _startTime).TotalMilliseconds.

While I don't expect the two to be identical, I was surprised to see them diverge linearly by about one millisecond per 20 seconds. I assume DateTime.Now calls the system clock, while the StopWatch does some kind of accumulation?

Sample output:

StopWatch : 0,2  DateTime : 1,0 Diff : 0,81
StopWatch : 10000,5  DateTime : 10002,6 Diff : 2,04
(...)
StopWatch : 2231807,5  DateTime : 2231947,7 Diff : 140,13
StopWatch : 2241809,5  DateTime : 2241950,2 Diff : 140,70

Full source: https://gist.github.com/knatten/86529563122a342de6bb

Output: https://gist.github.com/knatten/84f9be9019ee63119ee2

5
  • think about it... you can't call them both on the same line at exactly the same time... so yes, within your timer, the time it takes to print the information for datetime, and THEN print the information for StopWatch, regardless of how close calls they are (one right after the other), over 20 seconds, will show a relatively small difference (1ms)...
    – MaxOvrdrv
    Commented Apr 30, 2014 at 14:19
  • 5
    @MaxOvrdrv While this may account for an initial difference, it would not cause consistent drift as specified in the OP.
    – Ashigore
    Commented Apr 30, 2014 at 14:20
  • 1
    @Ashigore you're right... it should be the same difference seen in all the printouts... i don't know then... maybe his internal clock battery is running low on power? ;)
    – MaxOvrdrv
    Commented Apr 30, 2014 at 14:23
  • I would like to add for you that the differences between the two method should not concern you in production code. Hopefully, any timing dependent code will be managed using the synchronization primitives provided by .net!
    – Gusdor
    Commented Apr 30, 2014 at 14:50
  • 1
    Thanks for your comment @Gusdor. I am not using this for anything critical, and never actually compare them. I just got curious when I discovered it.
    – knatten
    Commented May 1, 2014 at 17:00

3 Answers 3

4

The answer is relatively straight forward.

  • Stopwatch counts processor ticks using a performance counter, a mechanism that varies between processors.
  • DateTime queries the system clock - the system clock is updated periodically by windows using output from the (probably quartz) crystal clock on your motherboard.

All clocks drift and these two different timing mechanisms will drift at different rates.

Under the hood, Stopwatch uses this API

The Stopwatch class assists the manipulation of timing-related performance counters within managed code. Specifically, the Frequency field and GetTimestamp method can be used in place of the unmanaged Win32 APIs QueryPerformanceFrequency and QueryPerformanceCounter.

DateTime uses this API

1
  • 2
    Not only do the physical clocks drift, but DateTime.(Utc)Now may additionally get readjusted to a network time source. See e.g. Hans Passant's answer to a similar question. Commented Mar 9, 2015 at 1:21
3

DateTime.Now ticks every few milliseconds. Also, the rate at which it ticks is not fixed from machine to machine. On modern windows systems machines you can expect that the tick resolution will be around 100 ticks per second.

On the other hand, StopWatch queries the CPU hardware to get high precision. Actually you can get the resolution of StopWatch using Stopwatch.Frequency.

I didn't know both of the above, until I read an interesting post of Eric Lippert, about common performance benchmarks mistakes, please look here here. It's really a great post.

3
  • By ticking, do you mean that DateTime.Now ticks and accumulates? I would expect it to query the system clock.
    – knatten
    Commented Apr 30, 2014 at 14:26
  • As I have understand, both DateTime.Now and StopWatch ticks but in different rates.
    – Christos
    Commented Apr 30, 2014 at 14:28
  • They tick at different rates, but it seems that Stopwatch accumulates ticks, while DateTime re-queries the system time, see Gusdor's answer.
    – knatten
    Commented Apr 30, 2014 at 14:37
0

Stopwatch is much more precise than DateTime which would account for the discrepancy

From MSDN:

The Stopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time. Otherwise, the Stopwatch class uses the system timer to measure elapsed time. Use the Frequency and IsHighResolution fields to determine the precision and resolution of the Stopwatch timing implementation.

2
  • 2
    So Stopwatch is more precise, but which one is more accurate? Do they both accumulate ticks, or does DateTime.Now query the system clock? In that case we might have a more precise Stopwatch, but a more accurate DateTime.
    – knatten
    Commented Apr 30, 2014 at 14:24
  • 1
    Accuracy will vary across hardware configurations.
    – Gusdor
    Commented Apr 30, 2014 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.