Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
679 views
in Technique[技术] by (71.8m points)

c++ - HPET's frequency vs CPU frequency for measuring time

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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.

Edit: 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...