C ++: FPS displacement calculation

I would like to calculate the FPS for the last 2-4 seconds of the game. What would be the best way to do this?

Thanks.

Edit: To be more specific, I have access to a timer in increments of one second.

+4
source share
4 answers

No wonder to miss a very recent publication. See My answer there using exponentially weighted moving averages.

C ++: counting the final frames in the game

Here is a sample code.

Initially:

avgFps = 1.0; // Initial value should be an estimate, but doesn't matter much. 

Every second (if the total number of frames in the last second is in framesThisSecond ):

 // Choose alpha depending on how fast or slow you want old averages to decay. // 0.9 is usually a good choice. avgFps = alpha * avgFps + (1.0 - alpha) * framesThisSecond; 
+9
source

Is it possible to save a circular frame time buffer for the last 100 frames and average them? This will be "FPS for the last 100 frames." (Or rather, 99, since you will not differ in the newest time and the oldest.)

Name some exact system time, milliseconds or better.

+1
source

Here is a solution that might work for you. I will write this in pseudo / C, but you can adapt the idea to your engine.

 const int trackedTime = 3000; // 3 seconds int frameStartTime; // in milliseconds int queueAggregate = 0; queue<int> frameLengths; void onFrameStart() { frameStartTime = getCurrentTime(); } void onFrameEnd() { int frameLength = getCurrentTime() - frameStartTime; frameLengths.enqueue(frameLength); queueAggregate += frameLength; while (queueAggregate > trackedTime) { int oldFrame = frameLengths.dequeue(); queueAggregate -= oldFrame; } setAverageFps(frameLength.count() / 3); // 3 seconds } 
+1
source

What you really want is something like this (in your mainLoop):

 frames++; if(time<secondsTimer()){ time = secondsTimer(); printf("Average FPS from the last 2 seconds: %d",(frames+lastFrames)/2); lastFrames = frames; frames = 0; } 

If you know how to handle structures / arrays, it will be easy for you to extend this example to 4 seconds instead of 2. But if you want to get more detailed help, you really need to indicate WHY you do not have access to the exact timer (which architecture, language ) - otherwise everything seems like a guess ...

0
source

Source: https://habr.com/ru/post/1335498/


All Articles