FPS counter solution?

Hi all!

I calculate the current fps in the following way:

  • measure time it takes to render a frame in ms and store it in a float array (fTime[5]) with an index variable iFrames
  • do iFrames++ every time the function is called (everytime a frame is rendered)
  • check if iFrames == 5
    • if TRUE: divide 1000.00f (1 sec) with the average time it took to render a frame
  • display the current float variable that holds the last framerate per second on the screen
  1. Is this a good solution?
  2. Should I average more than the last 5 rendered frames or is there too much overhead then?
  3. Are there better ways?
  4. Is there a function to average the float array (add all values and divide by the array size)?

Regards,
Diapolo

Oh, it’s simpler than your solution!!
You only need oldTime and fps variables and a counter initialized with 0. And something like that:

if (cnt++>=50) {
cnt=0;
fps=50.0*1000.0/(oldTime-functionThatGetsTime());
oldTime=functionThatGetsTime();
}

This in case you want to do the average of 50 frames at time.
You are doing a moving average that isn’t necessary…

tFz

Your routine is simpler, faster but not as precise as mine, or?
Would you say your solution is much faster or would you say it doesn´t really matter in this case?

And my other question (to everyone out there g), is there a function for averaging array content, that is faster compared to doing it myself?

Regards,
Diapolo

Teofuzz’s solution isn’t any less precise than yours and should be faster. Take your two formulas for example. Consider the math for a minute… in order to average the time for your formula, you have to add up the total time and divide by the number of times you have. Teofuzz’s inherently times only the total time for the number of frames you are calculating the FPS with. You basically toss out the individual time data when you average, so what’s the point of that?