- Home /
How to calculate an average frame rate
I made a simple FPS monitor script. It displays the current frame rate, as well keeps track of the lowest and highest numbers. I plan on keeping track of the average FPS for each level. My question is how do I calculate the average frame rate?
well to find an average, count say 1000 frames and notice how long it took.
You then need to use division.
Also -
http://docs.unity3d.com/Documentation/ScriptReference/Time.html
notice frameCount
FYI
http://docs.unity3d.com/Documentation/ScriptReference/Application-targetFrameRate.html
Answer by fafase · Oct 03, 2012 at 05:45 AM
An approximate calculation can be done with:
print(1/Time.deltaTime);
EDIT: Well, this is not approximate at all, this is the value. If you have been through some oscillations and waves topics, deltaTime is T or period which is the time between two peaks of the wave calculated in second. fps is the frequency f which is the amount of peaks in 1 second calculated in Hertz(Hz) and the relation with T is:
f = 1/T;
T = 1/f;
In our case a peak is a frame starting.
yeah, I wanted the average of all the values. Currently I'm thinking of recording each frame and dividing the total time played in each level. I was thinking there was a better way... To calculate the average right now, it would look like:
28800 frames / 1200 seconds = 24
I guess, now that I look at it, this method isn't so bad. Thanks.
What you are looking for then for average is there:
http://docs.unity3d.com/Documentation/ScriptReference/Time-realtimeSinceStartup.html
It says:
// A FPS counter.
// It calculates frames/second over each updateInterval,
// so the display does not keep changing wildly.
Answer by AnomalusUndrdog · Oct 10, 2013 at 11:30 PM
It's called Moving Average. The cumulative type of moving average is what you probably want. It's the average of all values up to the current one.
A brute force approach is to collect all fps values since the beginning and continually making an average of all of them. But through math, you can simplify the formula such that you don't need to collect the values anymore. You just keep the current average and how many have been averaged so far, and use some math to get the new one.
Check the wikipedia article linked for details.
Take note you still need to compute the FPS yourself using other means. What this does is only make an average of all FPS values so far.
int qty = 0;
float currentAvgFPS = 0;
float UpdateCumulativeMovingAverageFPS(float newFPS)
{
++qty;
currentAvgFPS += (newFPS - currentAvgFPS)/qty;
return currentAvgFPS;
}
Answer by Marrt · Nov 13, 2016 at 07:37 PM
This is how i do it, it is far easier than anything else, no cumulative stuff, just one float
float avg = 0F; //declare this variable outside Update
//run this in Update()
avg += ((Time.deltaTime/Time.timeScale) - avg) * 0.03f; //run this every frame
float displayValue = (1F/avg); //display this value
The good thing about this is that past values gradually fade out of importance, so it is not to jittery and not to slow
I find this answer better than the one provided by @AnomalusUndrdog: his solution will always take into account past values and new values will have smaller and smaller importance, resulting in a not accurate framerate
Your answer
Follow this Question
Related Questions
Real elapsed time since frame begin 1 Answer
How can I make a GUI element move independently of the frame rate? 1 Answer
Framerate counter 1 Answer
How to calculate the amount of Change of Position.Y for a certain amount of time. 0 Answers
My Character lags when moving, when i have a custom made blender model what should i do? 0 Answers