Finding FPS

How would I find the FPS of my program. Not the average, but just so I can display it on the screen while my program is running.

To get the current FPS rate, you need to have some function called exactly once every second (I dunno how in win32 nor in X). Have it do something like:
FPS_Rate = FPS_Counter;
FPS_Counter = 0;

Then every frame, increment FPS_Counter. FPS_Rate will contain the current FPS.

Here’s two useful functions for this:

__inline __int64 GetCycleNumber(){
__asm {
RDTSC
}
}

int GetMHz(){
LARGE_INTEGER t1,t2,tf;
__int64 c1,c2;

QueryPerformanceFrequency(&tf);
QueryPerformanceCounter(&t1);
c1 = GetCycleNumber();
_asm {
MOV EBX, 5000000
WaitAlittle:
DEC EBX
JNZ WaitAlittle
}
QueryPerformanceCounter(&t2);
c2 = GetCycleNumber();

return (int) ((c2 - c1) * tf.QuadPart / (t2.QuadPart - t1.QuadPart) / 1000000);
}

Call GetMHz in the beginning of your app and save the value. Then do like this:

void Init(){
MHz = GetMHz();
}

void render(){
static LARGE_INTEGER last,current;
double FPS;

last = current;
render_your_scene();
current = GetCycleNumber();

FPS = ((double) (current - last)) / (1000000.0 * MHz);

}

Does ANSI C not have a timer function that will allow me to know when one second is up?

if(timer()==1)
{
display FPS
reset FPS counter
reset timer
}

if ur making the program in windows, use settimer when your window is created. this will generate a WM_TIMER event every x amount of time (you set this to 1 second, of course). and when a WM_TIMER even occurs, simply save your framecount and reset the framecount to zero again.

You might also just take the average of say 10 frames.

start=time(NULL);
.
.
fpscounter++;
if(fpscounter >10)
{
fpscounter = 0;
end=time(NULL);
fps = 10 / (end - start);
start=time(NULL);
}

(untested, offcouz )

Anyway, that should work

–> Divide Overflow

[This message has been edited by Divide Overflow (edited 09-02-2000).]

well, on Windows you could try

render_function()
{
static long millisecs;
static int frames;
long t;

frames++;
if ((t =timeGetTime())-millisecs >= 1000) {
/* show the frames here */

frames = 0;
millisecs = t;
}

/* for a Linux/UNIX application try gettimeofday() */

or try this technique…

//pseudo code

gettime(a);
drawstuff();
gettime(b);
timediff=b-a; //in ms
fps=1000/timediff

THAT’S IT… Q.E.D.

What header file and/or libraries does the function timeGetTime() need?

When I use timeGetTime I get this error:
error LNK2001: unresolved external symbol __imp__timeGetTime@0

Thanks,
wolfman8k

[This message has been edited by wolfman8k (edited 09-07-2000).]

Hi wolfman8k!
The easiest technic (in my opinion) for windows is the Message from HUMUS.
you only have to use two Statements and three variables:

LARGE_INTEGER before, after, ClockPerSec;
QueryPerformanceFrequency(&ClockPerSec);

QueryPerformanceCounter(&before);
DrawWorld();
QueryPerformanceCounter(&after);

float duration = ((float)after.QuadPart-before.QuadPart ) / ClocksPerSec.QuadPart;

float FPS=1/duration;

So duration is the time between before and after in miliseconds. This is the same Algo like Humus, but a little bit shorter and not the exact one he had written. But if you have questions, look at his posting it will work…
Greets, Peter

What’s the difference between performanceCounter and performanceFrequency?

Thanks,
wolfman8k

The PerformanceFrequency returns a constant which is the number of times/sec the Counter is updated.