Is the timer resolution of the System.Diagnostics.Stopwatch class stable?

余生颓废 提交于 2019-12-02 07:46:35

MSDN:

*The Frequency value depends on the resolution of the underlying timing mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Frequency value reflects the frequency of that counter. Otherwise, the Frequency value is based on the system timer frequency.*

The source of the Stopwatch.Frequency is not the CPU frequency. It is the frequency of a high frequency hardware counter. On windows this counter frequency is typically also read by a call to the QueryPerformanceCounter function while on linux High Resolution POSIX Timers are used to get high resolution timing.

The use of Time Stamp Counter (RDTSC - CPU frequency) is often discussed in this context but has no real relevance:

Some older hardware may not provide any high frequency hardware for timing purposes, thus the frequency of the CPU is used as a replacement. When using the CPU frequency for timing it needs to be secured that the frequency does remain constant. More modern hardware provides dynamic CPU frequency modification. Using the CPU freqency as a measure while it is dynamically modified would end in a messs.

Fortunately this is non existing situation. Systems which can modify their CPU frequency are more modern. Such modern systems also do have a separate high frequency timer hardware High Precision Event Timer.

There are some rumors about using the CPU frequency on multicore systems. In fact there has been a problem with certain hardware. But that's long time ago and fixed for some time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!