Archived

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

How to Calculate FPS

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Although there are tons of tutorials for this, here is the code:

LARGE_INTEGER startTime;
LARGE_INTEGER ticksPerSecond;

// Initalize the FPS Counter
// If tis returns false, the FPS counter will be incorrect.
bool FPSCountInit()
{
if (!QueryPerformanceFrequency(&ticksPerSecond))
{
return false;
}
else
{
QueryPerformanceCounter(&m_startTime);
return true;
}
}

// FPSCountGetFPS returns the FPS Count. If this is not called
// every frame, you should track the number
// of frames yourself, and reset the value after this is called.
float FPSCountGetFPS(unsigned long elapsedFrames = 1)
{
static LARGE_INTEGER s_lastTime = startTime;
LARGE_INTEGER currentTime;

// Get actual time
QueryPerformanceCounter(¤tTime);

// Calculate FPS
float fps = (float)elapsedFrames * (float)ticksPerSecond.QuadPart / ((float)currentTime.QuadPart - (float)s_lastTime.QuadPart);

// Reset the counter
s_LastTime = currentTime;

return fps;
}


And you use it:

// Or what ever
BOOL InitGL()
{
...
if (!FPSCountInit())
return FALSE;
}

// Or what ever
DrawGLScene()
{
...
glFlush();

float fps = FPSGetFPSCount();
}


I hope, it helps.

PM

[edited by - PM on July 27, 2002 4:42:33 AM]

Share this post


Link to post
Share on other sites
a much more reliable way (and readable) is simply to count the number of frames you draw every second. It isn''t as accurate when the frame rate is very low, but is considerably more accurate when the frame rate is very high (ie, anything over 30fps)

it''s also easier as you do not need a high precision timer, even getTickCount() is accurate enough for the job.


example:


(this is off my head btw)

whack this in your draw code:


static int tick=GetTickCount(),frame=0,rate=0;
frame++;
if (GetTickCount()-tick>1000)
{
tick+=1000;
rate=frame;
frame=0;
}

Share this post


Link to post
Share on other sites