I am I calculating my FPS correctly?

Started by
6 comments, last by Moe 22 years, 2 months ago
I have a quick question and I am sure someone can answer it easily. I am calculating my frames per second by taking inverse of the difference in time between 2 frames, like so:
  
//suedocode...

float delta;
float fps;
currentime-previoustime = delta;

fps = 1.0/delta.
  
I am getting the current/previous time with timeGetTime() in the appropriate ways. Is this the correct way to calculate the frames per second? Moe''s Site
Advertisement
It depends on the units you''re measuring time in. Assuming delta is in milliseconds, change the last line from fps=1.0/delta to fps=1000/delta
It depends on the units you''re measuring time in. Assuming delta is in milliseconds, change the last line from fps=1.0/delta to fps=1000/delta
GetTickCount() returns milliseconds. So fps = 1000/delta.
--------------------Go Stick Your Head In A Pig
Here's a varient of the FPS algorithm found LaMonthe's "3D Game Programming" that I used for an SDL project.

float framesSoFar = 1;
float startTime = SDL_GetTicks();
float framesPerSec;
float nowTime;

while(!done)
{
nowTime = SDL_GetTicks();
framesPerSec = ((float)framesSoFar / (float)(nowTime - startTime)) * 1000;
printf("fps = %3.1f\n", framesPerSec);
framesSoFar++;
}


I think that I too, stared out with a simple frame-to-frame calculation, but I found it unstable and it didn't seem to give proper results. I've ran something like this for hours and hours on multiple OS's and machines and it seems fairly correct.

-Solstice

deninet.com
aeris.deninet.com

"...I was given three choices, the earth, the stars, or..."

Edited by - Solstice on February 7, 2002 1:15:40 AM
-Solsticedeninet.comaeris.deninet.com"...I was given three choices, the earth, the stars, or..."
If delta is measured in seconds: jes.
If delta is measured in milliseconds: no.
(to convert to seconds do this: milliseconds/1000)



Message from above:
Damn, my hair is grey!

Edited by - Gamekeeper on February 7, 2002 6:50:10 AM
Message from above:Damn, my hair is grey!
Yes, I think Delta is measured in seconds.

I just converted my code over from using timeGetTime() to the fancy PerformanceCounter thing. It definitly does have better precision. As soon as I get home I will post up the code on it to see if it is correct.

Moe''s Site
Ok, here is the code:

The main class object:
  class TIMER2{public:	LONGLONG CurrentTime;	DWORD TimeCount;	LONGLONG PerformanceFrequency;	LONGLONG NextTime;	LONGLONG PreviousTime;	double TimeElapsed;	double TimeScale;	float Delta;	int InitTimer();	int CheckOneSecondTimer();	int CheckTimer();	void UpdateTimer();};  


The functions:
  //--------------------------------------------------//// Name: InitTimer()// Desc: function that sets up the timer to 1/30 of a second//--------------------------------------------------//int TIMER2::InitTimer(){	//set some default values first	NextTime = 0;	PreviousTime = 0;	Delta = 0.0f;		//check for a performance counter	if(QueryPerformanceFrequency((LARGE_INTEGER *)&PerformanceFrequency))	{		//it got the performance frequency, so continue		TimeCount = (long)PerformanceFrequency/30;		QueryPerformanceCounter((LARGE_INTEGER *)&NextTime);		TimeScale = 1.0/PerformanceFrequency;	} else {		// it failed		return TIMER_BAD;	}	//save the time of the last frame	PreviousTime = NextTime;	//return ok	return TIMER_OK;}//--------------------------------------------------//// Name: CheckTimer()// Desc: function that checks the time to see if it //		is time to return yes//--------------------------------------------------//int TIMER2::CheckTimer(){	//check to see if it is time yet	if(CurrentTime >= NextTime)	{		//save the frame time		PreviousTime = CurrentTime;		//set the next time for the timer to fire		NextTime = CurrentTime + TimeCount;		return TIMER_OK;	}	else 		return TIMER_BAD;}//--------------------------------------------------//// Name: UpdateTimer()// Desc: function that updates the timer so that timeGetTime //		doesn''t have to be called so many times//--------------------------------------------------//void TIMER2::UpdateTimer(){	//calculate the new Delta value	Delta = (float)((CurrentTime-PreviousTime)*TimeScale);	//now update the previous time	PreviousTime = CurrentTime;	//update the current time	QueryPerformanceCounter((LARGE_INTEGER *)&CurrentTime);}  


Moe''s Site

This topic is closed to new replies.

Advertisement