Home » Community » Forums » Game Programming » Calculate Frames Per Second
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 Calculate Frames Per Second
Post New Topic  Post Reply 
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.

 User Rating: 1058   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

try something like:

int nFramesPerSecond=(int)(1000.f/fTimeDifference);

 User Rating: 1031   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1289   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1950   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

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,

-- Emmanuel D. [blog, in French] [blog, very bad googlized translation] [NEW: English version of teh blog! (WIP)]

 User Rating: 1828   |  Rate This User  Send Private MessageView ProfileView JournalView GD Showcase Entries Report this Post to a Moderator | Link

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.

 User Rating: 1803   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

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.

 User Rating: 1046   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Here is a simple tutorial showing FPS from GameTutorials.

 User Rating: 1933   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

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

 User Rating: 1058   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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?

 User Rating: 1058   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1090   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Technically, you only request a 1 ms update time. It will return TIMERR_NOCANDO if it isn't possible.

 User Rating: 1803   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

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.

 User Rating: 1197   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1138   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Here is an exampel of using QueryPerformanceFrequency()
__int64  timerFreq;           // How many counts in a second.
__int64  startTime, endTime;  // Start and ends times
float    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

 User Rating: 1016   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: