Skip to main content
GameDev.net gamedev.net
🔒 Locked

Calculate Frames Per Second

Started by utilae Jan 9, 2005 at 11:47 PM 13 replies 11.6k views
Original Post
utilae
utilae
Hi I want to calculate the frames per second, so i can display it on screen. Right now I do the following:

//start time
DWORD dwTimeStart=GetTickCount();
//.
//.after a while
//.
//time difference between start and end(now)
float fTimeDifference=GetTickCount()-dwTimeStart;
if(fTimeDifference==0)fTimeDifference=0.1;

//1000ms divide by time difference in ms
int nFramesPerSecond=1000/fTimeDifference;


My problem is that fTimeDifference=0, so I detect that and set it to 0.1, giving me 10,000 frames per second!!! I'm wondering if I am doing this wrong or something.
HTML5, iOS and Android Game Development using Corona SDK and moai SDK
uncutno
uncutno
try something like:

int nFramesPerSecond=(int)(1000.f/fTimeDifference);
-Anders-Oredsson-Norway-
mumpo
mumpo
What uncutno said. Also, if the actual time difference is very small, GetTickCount() may not be accurate enough for you. GetTickCount() returns miliseconds, but I seem to recall that it is not actually guaranteed to be updated at anything all that close to once a milisecond. You might try QueryPerformanceFrequency() and QueryPerformanceCounter() instead.
benryves
benryves
My solution is probably more accurate, but less instant:

int lastFPSlog = 0;
int frames = 0;
int fps = 0;

while (true) {

// Renderer code

++frames;

int time = SDL_GetTicks();
if (time > lastFPSlog+1000) {
fps = frames;
frames = 0;
lastFPSlog = time;
printf("FPS: %i\n",fps);
}
}

That way it only updates once a second, but is more accurate.
[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]
Emmanuel Deloget
Emmanuel Deloget
Quote:
Original post by mumpo
What uncutno said. Also, if the actual time difference is very small, GetTickCount() may not be accurate enough for you. GetTickCount() returns miliseconds, but I seem to recall that it is not actually guaranteed to be updated at anything all that close to once a milisecond. You might try QueryPerformanceFrequency() and QueryPerformanceCounter() instead.


To add more informations: GetTickCount() granularity is known by using GetSystemTimeAdjustment(). AFAIR it is close to 10ms.

Regards,
Zipster
Zipster
Use a wrapper class around your timer services. It you have a high-resolution timer, use QueryPerformanceCounter. If that fails, maybe use the multimedia timer (timeGetTime), which has the potential to be set to use a higher resolution than GetTickCount for a short time. And lastly, resort to GetTickCount.

However if this is just for a FPS counter, messing with high-resolution timers is just overkill. They'd be more useful if you were writing a code profiler where you actually have to measure each individual block of code. I personally just update my FPS counter once a second. You get a nice, clean value because you're counting frames ever second, not taking the inverse of elapsed time every frame.
Drethon
Drethon
Maybe I'm missing something but aren't your units in ms? So 1000/0.1 isn't 10,000 frames per second but 10,000 frames per millisecond = 10 frames per second.
My $0.02
Drew_Benton
Drew_Benton
Here is a simple tutorial showing FPS from GameTutorials.
utilae
utilae
Quote:
Original post by Drethon
Maybe I'm missing something but aren't your units in ms? So 1000/0.1 isn't 10,000 frames per second but 10,000 frames per millisecond = 10 frames per second.

1000ms = 1 second

1000ms/1000ms = 1 frame for this second, ie 1 fps

EDIT: I am using c++ and Direct3D9
HTML5, iOS and Android Game Development using Corona SDK and moai SDK
utilae
utilae
Quote:
Original post by benryves
My solution is probably more accurate, but less instant:

int lastFPSlog = 0;
int frames = 0;
int fps = 0;

while (true) {

// Renderer code

++frames;

int time = SDL_GetTicks();
if (time > lastFPSlog+1000) {
fps = frames;
frames = 0;
lastFPSlog = time;
printf("FPS: %i\n",fps);
}
}

That way it only updates once a second, but is more accurate.

Hey, I was wondering. The bit were you have 'while(true)'. While what is true?
HTML5, iOS and Android Game Development using Corona SDK and moai SDK
nife
nife
timeGetTime can get 1 ms accuracy by using timeBeginPeriod(1); in create function and timeEndPeriod(1); in destroy function. It's very important to remember the last one, because normally it only updates every 10 ms or so.
Killers don't end up in jailThey end up on a high-score!
Zipster
Zipster
Technically, you only request a 1 ms update time. It will return TIMERR_NOCANDO if it isn't possible.
GodlyGamr
GodlyGamr
Quote:
Hey, I was wondering. The bit were you have 'while(true)'. While what is true?


Nothing in particular. It will just keep looping until the program is terminated.
helix
helix
Quote:
Original post by utilae
Hey, I was wondering. The bit were you have 'while(true)'. While what is true?


That's just a way to make an infinite loop. true is just a statement the same as any other conditional test that will resolve to a true or false. In his example, that's just the game loop that will run forever until the game exits.
MrRage
MrRage
Here is an exampel of using QueryPerformanceFrequency()
__int64  timerFreq;           // How many counts in a second.__int64  startTime, endTime;  // Start and ends timesfloat    fps;if(QueryPerformanceFrequency((LARGE_INTEGER *)&timerFreq) != 0){  QueryPerformanceCounter((LARGE_INTEGER *)&startTime);  ....  QueryPerformanceCounter((LARGE_INTEGER *)&endTime);  fps = (float)((double)timerFreq / ((double)(endTime - startTime)));}


Thant should do it

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.