HPET的频率与CPU频率的测量时间 [英] HPET's frequency vs CPU frequency for measuring time

查看:923
本文介绍了HPET的频率与CPU频率的测量时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测量函数执行时间(以纳秒为单位)。现在我想了解,如果我的电脑可以做到这一点,什么是测量的精度。有一个建议使用QueryPerformanceFrequency()获取HPET的频率,1 / hpetFrequency是可以测量的原子时间。这是正确的吗?我的意思是如果我的CPU频率是3.33 GH,甚至1GHz的一个时钟持续时间是一纳秒。是不是意味着我可以测量纳秒?

I need to measure function execution time in nanoseconds. Now I want to understand if my computer can do that and what is the precision of measurement. There was a suggestion to use QueryPerformanceFrequency() to obtain the HPET's frequency and 1/hpetFrequency is the atomic time that can be measured. Is this right? I mean if my cpu frequency is 3.33 GH, even 1GHz that one clock duration is one nanoseconds. Doesn't it mean that I can measure by nanoseconds?

测量时间CPU或HPET的频率是必不可少的,为什么?

For measuring time CPU or HPET's frequency is essential and why?

推荐答案

你不能测量到1纳秒,你也不能测量到10纳米。这是因为每个测量操作需要某种类型的调用。最快的API之一是
GetSystemTimeAsFileTime()。呼叫需要10-15ns。但是它的分辨率/粒度相当差(在ms模式)。 QueryPerformanceCounter()提供MHz到GHz范围内的频率,具体取决于底层硬件。这个调用不是那么快,但在1MHz,你得到1微秒的分辨率。在由QueryPerformanceFrequency()给出的频率上,连续调用将返回相等的值,因为调用比增量速率快。
另一个来源是CPU时间戳计数器(rdtsc)。但也有一些缺点:现代硬件实现适应性的CPU频率。因此,该频率不能被认为是常数。这种方式只有在恒定阶段才可以进行测量。

You cannot measure to 1 nanosecond, you cannot measure to 10 nanosconds either. This is because each action of measurement requires a call of some kind. One of the fastest APIs is GetSystemTimeAsFileTime(). A call requires 10-15ns. But it's resolution/granularity is rather poor (in the ms regime). QueryPerformanceCounter() delivers frequencies in the MHz to GHz range, depending on the underlaying hardware. This call is not as fast but at 1MHz you get 1 microsecond resolution. At such a frequency, given by QueryPerformanceFrequency(), consecutive call will may return equal values because the call is faster than the increment rate. Another source is the CPU time stamp counter (rdtsc). But there are some drawback with it too: Modern hardware implements adpative CPU frequency. Therefore this frequency cannot be considered as a constant. This way measurements are only posible during constant phases.

事实上,没有一个频率源提供恒定频率。所有这些频率由具有偏移和漂移的某些硬件产生。因此,操作系统将返回QueryPerformanceFrequency或CPU频率的值,并使您相信它是一个常数。但是,您会得到的数字只有关闭估计

In fact none of the frequency sources delivers a constant frequency. All of these frequencies are generated by some hardware which has offset and drift. So the OS will return a value for QueryPerformanceFrequency or CPU frequency and makes you believe it is a constant. However, the number you'll get are only close estimates.

真正准确的时间只能在这些频率针对系统RTC进行校准。有关窗口的准确计时的详细信息,请参见出版物。

Real accurate timing can only be performed when these frequencies are calibrated against the systems RTC. See this publication for more detailed information about accurate timing on windows.

编辑:Windows选择cpu的时间戳计数器。在这种情况下,QPF()的结果等于处理器速度除以固定数(在你的情况下为1024)。 Windows选择在TSC周围建立计时,优先考虑常量/不变TSC可用。 3.33 GHz / 1024 = 3.25 MHz。

Windows chooses the Time Stamp Counter of the cpu. In such cases the result of QPF() equals the processor speed divided by a fixed number (1024 in your case). Windows chooses to built the timekeeping around the TSC with preference when a constant/invariant TSC is available. 3.33 GHz/1024=3.25 MHz.

这篇关于HPET的频率与CPU频率的测量时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆