Public Group

# Performance Timing

This topic is 4699 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

About a year ago when i was trying to write game without any knowlege about the way C works, i used a performance timer that i got working (and wrote myself) ... posted below:
#include "timer.h"

LARGE_INTEGER timerLast;
LARGE_INTEGER timerThis;
LARGE_INTEGER timerFreq;

void TimerPrep(void)
{
QueryPerformanceFrequency(&timerFreq);
QueryPerformanceCounter(&timerThis);
}

LONGLONG TimerTick(void)
{
// LAST = THIS so that THIS can be updated and the difference returned
timerLast = timerThis;
// Update THIS
QueryPerformanceCounter(&timerThis);
// Return the Difference
}
But i am battling to work with and display the LONGLONG used in this function and TBH i dont like this code because i'm continually having to mess around with it. Is there any very easy to use high resolution timing header available? I really need a high resolution timer without the fuss :-( Any input would be appreciated. Thanks :-)

##### Share on other sites
This doesn't answer your question, but I've heard that QueryPerformanceCounter/QueryPerformanceFrequency shouldn't be used anymore (the timer frequency changes mid-program). I could easily be wrong on this, so I'd wait for someone else to confirm it.

##### Share on other sites
Quote:
 Original post by Daniel MillerThis doesn't answer your question, but I've heard that QueryPerformanceCounter/QueryPerformanceFrequency shouldn't be used anymore (the timer frequency changes mid-program). I could easily be wrong on this, so I'd wait for someone else to confirm it.

The QueryPerformanceFrequency function retrieves the frequency of the high-resolution performance counter, if one exists. The frequency cannot change while the system is running.

##### Share on other sites
Quote:
 Original post by Daniel MillerThis doesn't answer your question, but I've heard that QueryPerformanceCounter/QueryPerformanceFrequency shouldn't be used anymore (the timer frequency changes mid-program). I could easily be wrong on this, so I'd wait for someone else to confirm it.

This is mostly an issue with laptops, I believe. The processors can actually slow themselves down in order to conserve energy, but Windows queries the frequency once at startup and just remembers the value.

To the OP: I am unaware of anything that would help. I believe VC++ can automagically print LONGLONG values using cout, but I don't recall for sure. If not, create a string just like any other integer type. [hint: cout << x%10; x /=10...there are gotcha's, but that's a start]

CM

##### Share on other sites
Quote:
Original post by Conner McCloud
Quote:
 Original post by Daniel MillerThis doesn't answer your question, but I've heard that QueryPerformanceCounter/QueryPerformanceFrequency shouldn't be used anymore (the timer frequency changes mid-program). I could easily be wrong on this, so I'd wait for someone else to confirm it.

This is mostly an issue with laptops, I believe. The processors can actually slow themselves down in order to conserve energy, but Windows queries the frequency once at startup and just remembers the value.

To the OP: I am unaware of anything that would help. I believe VC++ can automagically print LONGLONG values using cout, but I don't recall for sure. If not, create a string just like any other integer type. [hint: cout << x%10; x /=10...there are gotcha's, but that's a start]

CM

'eh ... i dont use VC++ and its written in C, so printf is how its all being displayed.

A lot of people have games that use timers, does anyone wanna share exactly which one they used?

Thanks for you input you two :-)

##### Share on other sites
Ok, so i found a solution. Casting the LONGLONG stored in the _LARGE_INTEGER's QuadPart (man, that sounds wierd) to a DOUBLE which is really just a 8 byte integer where the second 4 bytes allow for decimal precision, you can do the math and return a float :-) which is much easier to use than a LARGE_INTEGER :-)

Anyways, posted below is a simple example on how to get this beast rolling.

#include <stdio.h>#include <stdlib.h>#include <windows.h>LARGE_INTEGER timerLast;LARGE_INTEGER timerThis;LARGE_INTEGER timerFreq;int timerReady = 0;/* * * */void TimerPrep(void){	QueryPerformanceFrequency(&timerFreq);	QueryPerformanceCounter(&timerThis);	timerReady = 1;}/* * * */float TimerTick(void){	if(timerReady == 0) {return 0;}	// LAST = THIS so that THIS can be updated and the difference returned	timerLast = timerThis;	// Update THIS	QueryPerformanceCounter(&timerThis);	// Return the Difference	return (((double)timerThis.QuadPart - (double)timerLast.QuadPart) / (double)timerFreq.QuadPart);}/* * * */int main(int argc, char *argv[]){  float laa;  TimerPrep();  system("PAUSE");  laa = TimerTick();  printf("Took %f\n", laa);  //printf("%d %d", sizeof(int), sizeof(double));  system("PAUSE");  return 0;}

Comments or improvements on this are appreciated :-) Thanks.

##### Share on other sites

Quote:
Original post by Conner McCloud
Quote:
 Original post by Daniel MillerThis doesn't answer your question, but I've heard that QueryPerformanceCounter/QueryPerformanceFrequency shouldn't be used anymore (the timer frequency changes mid-program). I could easily be wrong on this, so I'd wait for someone else to confirm it.

This is mostly an issue with laptops, I believe. The processors can actually slow themselves down in order to conserve energy, but Windows queries the frequency once at startup and just remembers the value.

So it's not an issue with laptops. :)
It's guaranteed not to change, *except* some motherboards have a bug that lets the QPC suddenly skip ahead by a large amount.
There's a MSDN article about it somewhere...

1. 1
2. 2
frob
20
3. 3
JoeJ
20
4. 4
5. 5

• 10
• 11
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632210
• Total Posts
3004812

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!