How to know FPSs?

Hi all,

What’s the best way to know the frames per second of my opengl application?

Best regards,

Paulo Matos

just create a counter variable and add 1 to it every time you display a frame. Then every 1 of couple of seconds, divide by the number of seconds that have passed. It probably best to just do it every second…

Originally posted by r_shadrach:
just create a counter variable and add 1 to it every time you display a frame. Then every 1 of couple of seconds, divide by the number of seconds that have passed. It probably best to just do it every second…

Do you mean to put a counter in the end of each display callback?

But if I’m always dividing for each frame I’ll have a division per frame which will just bring programs efficiency down!

Best regards,

Paulo Matos

You can do it like this:

void RenderScene()
{
static UINT uiFramesCounts = 0;
static DWORD dwLastTime = 0;

// Render here all the stuff you want

// Add 1 to indicate that next frame was shown

uiFrameCounts++;

// Check if 1 second passed

if ( dwLastTime-GetTickCount() >= 1000 )
{
// uiFrameCounts now shows FPS value
// which you can show on screen or whatever

  // Reset frames counter

  uiFrameCounts = 0;

  // Store time last FPS show was done

  dwLastTime = GetTickCounts();

}
}

Originally posted by pmatos:
But if I’m always dividing for each frame I’ll have a division per frame which will just bring programs efficiency down!
What are you talking about!?

Worst case FDIV is 24 cycles on an Athlon.
Worst case expected target machine is 500 MHz.

That’s twenty goddamn million float divisions per second and you’re talking about losing efficiency because of doing it once per frame? Grrr …

Can I put this piece of code in the registered idle function (if using glut)?

Is idle function called once per frame? (if not, then I cannot put the code there since I need a function that’s called once per frame, right?)

Best regards,

Paulo Matos

Originally posted by enmaniac:
[b]You can do it like this:

[quote]

void RenderScene()
{
static UINT uiFramesCounts = 0;
static DWORD dwLastTime = 0;

// Render here all the stuff you want

// Add 1 to indicate that next frame was shown

uiFrameCounts++;

// Check if 1 second passed

if ( dwLastTime-GetTickCount() >= 1000 )
{
// uiFrameCounts now shows FPS value
// which you can show on screen or whatever

  // Reset frames counter

  uiFrameCounts = 0;

  // Store time last FPS show was done

  dwLastTime = GetTickCounts();

}
}

[/b][/QUOTE]

The idle function is called when there is “some time to spare”, don’t use it.

Of course you have something that is called “once per frame”, you must render something wright ? put the code in there.

Mikael

Originally posted by r_shadrach:
just create a counter variable and add 1 to it every time you display a frame. Then every 1 of couple of seconds, divide by the number of seconds that have passed. It probably best to just do it every second…

I don’t do that because it’s not uncommon to have the driver at work behind the scenes in the first few seconds of rendering. This is like an average ratio. I compute istant ratio between frames (I don’t even require the division for who’s interested to performance to this degree, I can do it with a mul and if you are smart with even less - i am not)…

The advantage of this is a more responsive FPS counter. So, if you get 999fps for a long time then pop in some heavyweight geometry which takes you down to 9fps, you immediatly see the difference. Using average fps would put the counter on 998 and this would look bad.

Having a per-frame period also have other useful advantages (I am sure most games are using this approach).

I think this is the approach taken by the above code (modulo some typos…).

Originally posted by Obli:
[b] I don’t do that because it’s not uncommon to have the driver at work behind the scenes in the first few seconds of rendering. This is like an average ratio. I compute istant ratio between frames (I don’t even require the division for who’s interested to performance to this degree, I can do it with a mul and if you are smart with even less - i am not)…

The advantage of this is a more responsive FPS counter. So, if you get 999fps for a long time then pop in some heavyweight geometry which takes you down to 9fps, you immediatly see the difference. Using average fps would put the counter on 998 and this would look bad.

Having a per-frame period also have other useful advantages (I am sure most games are using this approach).[/b]

I think:

if ( 999 < (GetTickCount() - dwLastTime) )

works a bit better. :wink:

Originally posted by pmatos:
I think this is the approach taken by the above code (modulo some typos…).

Yes, looks out it is, I just wanted to point out some good reasons to do that instead of the averaging method.

Sorry to be a little late, but not the division is taking the time, but updating the caption of the window (if that is where you display your frame counter is).

I have made a FPS counter and showing it every other second or every other 100th frame improved my rendering speed. I have seen a 10x increase in framerate (from 50 to 500 or so).

To get rid of that and show the FPS every frame you could render the fps as text in your opengl window. (that’s what I have done)

Hi,

You can get a frame per second demo in this url… http://myopendemo.hypermart.net/opengl2.htm