I have been attempting to create a timer for my game and I heard about QueryPerformanceCounter and QueryPerformanceFrequency. Could someone please explain how these can be used to calculate time/fps/ticks in a game loop?.
Asked
Active
Viewed 9,724 times
-1
-
You should be aware that the BIOS/HAL in many PCs doesn't bother to sync the counters across cores/cpus, so samples taken on different cores reflect not only elapsed time but also a constant delta. With a bit of work, you can calculate these deltas at program startup and use the CPUID instruction to correct timings. Further, CPUs that change their speed adaptively frustrate timing with these clock-cycle counters. – Tony Delroy May 17 '11 at 06:55
-
1http://stackoverflow.com/questions/1739259/how-to-use-queryperformancecounter – DuckMaestro Jan 23 '12 at 08:13
2 Answers
11
Microsoft support has a Knowledge Base article specifically about this:
How To Use QueryPerformanceCounter to Time Code
Basically you use QueryPerformanceCounter
to get a high resolution timer value before and after the event you want to time.
Then use QueryPerformanceFrequency
to get the number of ticks per second. Divide the time difference by this value to convert the value to seconds.

Bo Persson
- 90,663
- 31
- 146
- 203
-
hey..sorry for the lack of research...i had no idea what to google to get the result i wonted...thanks for the help – I Phantasm I May 19 '11 at 08:52
5
LARGE_INTEGER m_liPerformanceFrequency;
QueryPerformanceFrequency( &m_liPerformanceFrequency);
//...
LARGE_INTEGER liPerformanceCount;
QueryPerformanceCounter( &liPerformanceCount);
double dTime = double(liPerformanceCount.QuadPart)/double(m_liPerformanceFrequency.QuadPart);

Henrik
- 23,186
- 6
- 42
- 92