Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am reading code of the timing system of Cyclone (a sample Ian Millington make to demonstrate about physics engine). And there I reach 1 function I have never seen before, QueryPerformanceFrequency.
Here the function that used QueryPerformanceFrequency:

C++
void initTime()
{
    LONGLONG time;

    qpcFlag = (QueryPerformanceFrequency((LARGE_INTEGER*)&time) > 0);

    // Check if we have access to the performance counter at this
    // resolution.
    if (qpcFlag) qpcFrequency = 1000.0 / time;
}


I just don't understand this line
C++
if (qpcFlag) qpcFrequency = 1000.0 / time;

I just can guess that the WinAPI get the CPU frequency and stored it in the var time. But as we can see, gpcFrequency is really the frequency. So what exactly the WinAPI do?

One extra question, I still find hard to understand the code of Cyclone, especially the timing data in the file timing.cpp and timing.h. I can understand the physic code like velocity and position but the timing data is such an strange theory to me. Can anyone recommnend any explanation about timing in game engine, a book or an article is ok. Thank you.
Posted
Updated 26-Mar-18 5:50am
Comments
Santhosh G_ 19-Oct-12 23:33pm    
Is there any calls to QueryPerformanceCounter in your code ?
Sergey Chepurin 20-Oct-12 5:07am    
As you asked "Game Timing and Multicore Processors" -http://msdn.microsoft.com/en-us/library/ee417693%28VS.85%29.aspx

Normally QueryPerformanceCounter and QueryPerformanceFrequency is used to get the accurate time of an operation.

QueryPerformanceCounter() provides current value of the high resolution timer.
and
QueryPerformanceFrequency returns the current performance-counter frequency, in counts per second.
Both of these function provides information of high-resolution performance counters in the system.

C++
qpcFlag = (QueryPerformanceFrequency((LARGE_INTEGER*)&time) > 0);
if (qpcFlag) qpcFrequency = 1000.0 / time;


Here qpcFlag indicates the presence of a High resolution timer in your machine. time is the counts per second of High Resolution Timer. Therefore qpcFrequency is the frequency in milliseconds.

An example of measuring time of an operation.
C++
QueryPerformanceCounter( &StartTime );
// Some Processing...
..
QueryPerformanceCounter( &EndTime );


After that you can measure the time of Processing in milliseconds by the following code.
C++
double fTime = (double)((EndTime.QuadPart - StartTime.QuadPart ) * qpcFrequency );


Details of QPC and QPF
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx[^]

http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx[^]
 
Share this answer
 
v2
Comments
Espen Harlinn 20-Oct-12 18:52pm    
5'ed! That's a nice reply :-D
Rick York 2-Mar-19 11:54am    
You have the terminology reversed. It should be : Hide   Copy Code
QueryPerformanceFrequency((LARGE_INTEGER*)&freqency) > 0); if (qpcFlag) qpcPeriod = 1000.0 / frequency;
QPF gives the frequency, as the name implies. The reciprocal of the frequency is the period, in milliseconds since it is scaled 1000.
The answer is that the Cyclone code is confusing! The return value of QPF is indeed a frequency in counts/sec, so returning to a variable called 'time' is unhelpful. The value of qpcFrequency, represented by the expression '1000.0/time', is then actually the 'counter interval' in milli-seconds. The QPC function returns a count, so the time between two events is (count2 - count1)*'counter interval' and is measured in milliseconds.

I only bothered to make this comment, as I have just been using QPF & QPC (in VB6 !, prior to porting the bits to LabWindows), and came across this question when trying to learn more about the function - as a long-time browser of the Lounge in this forum, I felt it was time I contributed, even if a bit late!

Will anyone see this?
 
Share this answer
 
Comments
HilmerDek 25-Oct-18 21:23pm    
yes, thanks
Nelek 2-Mar-19 15:16pm    
Yes... it is being seen.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900