windows clock()、gettickcount()、QueryPerformanceCounter() 和QueryPerformanceFrequency() 之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1559864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between clock(), gettickcount(), QueryPerformanceCounter() and QueryPerformanceFrequency()?
提问by Mike Dimmick
Do they all give millisecond resolution?
他们都给出毫秒分辨率吗?
回答by Mike Dimmick
No, they don't have millisecond precision. clock
and GetTickCount
have between 4 and 15 millisecond precision on most systems. QueryPerformanceCounter
has a precision in the microsecond to nanosecond range.
不,它们没有毫秒精度。clock
并且GetTickCount
在大多数系统上具有 4 到 15 毫秒的精度。QueryPerformanceCounter
精度在微秒到纳秒范围内。
clock
is a wrapper around GetTickCount
if you're using Microsoft's C runtime library, which, if you're using an MS compiler, you probably are. GetTickCount
returns a value in milliseconds, but it doesn't increase by one millisecond after one millisecond has elapsed. It is only incremented when a system clock interrupt occurs, which is every four to 15 milliseconds - normally it's about 15ms, but it can be changed by applications. This affects the whole computer: it affects thread scheduling, and the overhead of more frequent interrupts also leaves less CPU time for actually running program code, so don't do it unless you really need it. If your computer is ticking more frequently than 15ms, some other application has modified the tick interrupt frequency.
clock
GetTickCount
如果您使用的是 Microsoft 的 C 运行时库,则是一个包装器,如果您使用的是 MS 编译器,则可能是。GetTickCount
以毫秒为单位返回一个值,但一毫秒过去后它不会增加一毫秒。它仅在发生系统时钟中断时递增,每 4 到 15 毫秒一次 - 通常约为 15 毫秒,但可以由应用程序更改。这会影响整个计算机:它会影响线程调度,并且更频繁中断的开销也为实际运行程序代码留下更少的 CPU 时间,所以除非你真的需要它,否则不要这样做。如果您的计算机的滴答频率超过 15 毫秒,则其他一些应用程序已修改滴答中断频率。
QueryPerformanceCounter
uses whatever high-resolution timers are available on the system. In the past, it was usually based on the processor's internal count of clock cycles, so would count at 2-3GHz, or about 0.5ns. Unfortunately some processors varied the rate that the counter would tick at when in low-power states, and in multi-CPU systems (i.e. with multiple processor sockets) you'd get problems if the CPUs weren't all the same speed. Windows now uses other more reliable sources, but they aren't quite as high resolution as the processor. QueryPerformanceFrequency
tells you how many ticks occur in one second. To get milliseconds, multiply the difference of QPC samples by 1000, and divide by the result of QueryPerformanceFrequency
.
QueryPerformanceCounter
使用系统上可用的任何高分辨率计时器。过去,它通常基于处理器的内部时钟周期计数,因此会以 2-3GHz 计算,即大约 0.5ns。不幸的是,一些处理器会改变计数器在低功耗状态下的计数率,而在多 CPU 系统(即具有多个处理器插槽)中,如果 CPU 的速度不同,您就会遇到问题。Windows 现在使用其他更可靠的来源,但它们的分辨率不如处理器那么高。QueryPerformanceFrequency
告诉您一秒钟内发生了多少滴答声。要获得毫秒,请将 QPC 样本的差异乘以 1000,然后除以 的结果QueryPerformanceFrequency
。
回答by Daniel Rikowski
clock
clock
A C++ function. It returns the number of CPU tickssince the application is started. To convert this into seconds divide it by CLOCKS_PER_SEC
.
一个 C++ 函数。它返回自应用程序启动以来的CPU 滴答数。要将其转换为秒,除以CLOCKS_PER_SEC
.
GetTickCount
GetTickCount
A Win32 API function. Returns the number of millisecondssince the system was started.
Win32 API 函数。返回自系统启动以来的毫秒数。
QueryPerformanceCounter
QueryPerformanceCounter
A Win32 API function. The performance counter is a high-resolution counterwhich increases over time. To convert this into seconds divide it by QueryPerformanceFrequency
.
Win32 API 函数。性能计数器是一个高分辨率计数器,它会随着时间的推移而增加。要将其转换为秒,除以QueryPerformanceFrequency
.