问题
Let's say I have Stopwatch
running in my code and in the middle, some other application has changed the system clock (I have Domain Time on my server syncing every second).
Will it affect the stopwatch result?
回答1:
No, internally Stopwatch relies on Win32 QueryPerformanceCounter() which is not tied to the system clock.
回答2:
It depends.
Stopwatch
relies on Win32 QueryPerformanceCounter() only if it is available. This implementation is timechange tolerant.
In case high-resolution counters are not available, Stopwatch
falls back to system timer, basically DateTime.UtcNow.Ticks
call. In this unfortunate case Stopwatch
results will be affected by time(not timezone) changes.
As it is written in cited MSDN article, you can check if high-resolution counters are available using IsHighResolution
field.
You can blame .Net team using this won't fix defect :)
回答3:
No, it won't affect it.
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.
来源:https://stackoverflow.com/questions/14079523/will-a-system-clock-adjust-affect-running-the-stopwatch-in-c