Archived

This topic is now archived and is closed to further replies.

johnnyBravo

How to use QueryPerformance Counter/Frequency for timer?

Recommended Posts

Hi, I want to use the QueryPerformance Counter/Frequency apis to create a timer. But in all the examples that I have seen, i get lost. So far I''ve made an extremely inaccurate timer timeGetTime. So with a function, like the one that i made before, it goes like this. bool Timer(int duration) so to use it I''d say: if (timer) dowhatever I want to do this with the QueryPerformance apis. So how would i do this. Could someone explain how these api''s work? or direct me to a website. Thanks.

Share this post


Link to post
Share on other sites
QueryPerformanceCounter() gives you the number of "clock ticks" that have occurred since the system was turned on.

QueryPerformanceFrequency() gives you how many of these "clock ticks" there are in one second.

By taking the value of the counter and dividing by the frequency, you get the number of seconds that have elapsed since the system was turned on.

The values you get from these functions are 64-bit integers - check out the LARGE_INTEGER entry in MSDN for details.


[edited by - MisterMoot on July 28, 2003 2:25:28 AM]

Share this post


Link to post
Share on other sites
Use QueryPerformanceFrequency to get the frequency of the high-resolution timer (ie. the number of ticks per second).

Then use QueryPerformanceCounter to get the current tick count.

Then simply take the difference between the tick counts and divide by the frequency:



LARGE_INTEGER freq = QueryPerformanceFrequency();
LARGE_INTEGER time1 = QueryPerformanceCounter();

...
...

LARGE_INTEGER time2 = QueryPerformanceCounter();

double deltaT = (time2.QuadPart - time1.QuadPart) / freq.QuadPart;

Share this post


Link to post
Share on other sites