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
The answer is relatively straight forward.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With