It is probably best to use the average for a small number of frames. You mentioned that you want to calculate the average value, but there really is no reason to keep so many samples around to calculate the average value. Just keep the current number of frames for a short period of time (where small can be between 10-50 frames - we usually use 16). You can then use this sum to calculate average frames per second. This method also helps smooth out frame time reports so that numbers don't skip everywhere. One thing to keep in mind is that if you average too much time, then the framerate bursts become more βhiddenβ, which means that it can be more difficult to identify frames that cause a drop in frame rate if these frames only happen so often.
Something like this would be sufficient, I think (without checking the code):
// setup some variables once const int Max_samples = 16; // keep at most 16 frametime samples int FPS_Samples = 0; int Current_sample = 0; int Total_frametime = 0.0f; float Frametimes[Max_samples]; for ( int i = 0; i < Max_samples; i++ ) { Frametimes[i] = 0.0f;
Then, when you calculate your cycle time, you can do something like this:
// current_frametime is the new frame time for this frame Total_frametime -= Frametimes[Current_sample]; Total_frametime += current_frametime; Frametimes[Current_sample] = current_frametime; Current_sample = ( Current_sample + 1 ) % Max_samples; // move to next element in array Frames_per_second = Max_samples / Total_frametime;
This is a gross reduction and probably can use some error checking, but it gives a general idea.
source share