- Home /
How do I find the frames per second of my game?
Would I use Time.deltaTime somehow? I'd like to find the frames per second of my game in a standalone build, so I can't just look at "Stats" in the Editor.
Answer by taoa · Feb 08, 2011 at 04:33 PM
display the value
1.0f / Time.deltaTime
It'll give you a good approximation of your FPS.
However this will only tell you how many frames per second your frame was running at during the last frame! Consequently, this is very sensitive with even tiny variations in the time it took to render last frame.
Another popular method that might be more accurate, would be to know what was the framerate over a fixed amount of time.
Decide upon this amount of time, and just count how many frames have been rendered during that time:
//Declare these in your class int m_frameCounter = 0; float m_timeCounter = 0.0f; float m_lastFramerate = 0.0f; public float m_refreshTime = 0.5f;
void Update() { if( m_timeCounter < m_refreshTime ) { m_timeCounter += Time.deltaTime; m_frameCounter++; } else { //This code will break if you set your m_refreshTime to 0, which makes no sense. m_lastFramerate = (float)m_frameCounter/m_timeCounter; m_frameCounter = 0; m_timeCounter = 0.0f; } }
then display somewhere the value
m_lastFramerate
which will be the average framerate over the refresh time you'd have chosen (here 0.5 second)
It doesn't seem to be giving me accurate results... Is the code outdated or something. I Entered the code exactly as it is and hooked it up to a .Text$$anonymous$$esh but i'm getting odd results. Nothing incredable extraordinary, but, just inaccurate...
Answer by ZeroKcm · Aug 01, 2013 at 09:01 PM
More simple:
void OnGUI()
{
GUI.Label(new Rect(0, 0, 100, 100), (int)(1.0f / Time.smoothDeltaTime));
}
Answer by TimCoster · Feb 25, 2019 at 01:20 PM
Old topic, but there is a FPSCounter script in the unity standard assets utility folder..
using System;
using UnityEngine;
using UnityEngine.UI;
namespace UnityStandardAssets.Utility
{
[RequireComponent(typeof (Text))]
public class FPSCounter : MonoBehaviour
{
const float fpsMeasurePeriod = 0.5f;
private int m_FpsAccumulator = 0;
private float m_FpsNextPeriod = 0;
private int m_CurrentFps;
const string display = "{0} FPS";
private Text m_Text;
private void Start()
{
m_FpsNextPeriod = Time.realtimeSinceStartup + fpsMeasurePeriod;
m_Text = GetComponent<Text>();
}
private void Update()
{
// measure average frames per second
m_FpsAccumulator++;
if (Time.realtimeSinceStartup > m_FpsNextPeriod)
{
m_CurrentFps = (int) (m_FpsAccumulator/fpsMeasurePeriod);
m_FpsAccumulator = 0;
m_FpsNextPeriod += fpsMeasurePeriod;
m_Text.text = string.Format(display, m_CurrentFps);
}
}
}
}
Answer by Rodaraujo · Mar 12, 2013 at 11:15 PM
wouldn't Debug.Log(1/Time.deltaTime); be enough?
deltaTime can vary a great deal, so the numbers can be all over the place...especially if you have an app under load. I calculate an average similar to above, and I also use Time.smoothDeltaTime for my fps script. Debug.Log() outputting to the console will also slow the app, so it is better to output to the screen.
Yes maybe, but if you use fixedupdate it should stay on average Time. DeltaTime
If you use FixedUpdate() you're not measuring the framerate of your game; you're measuring your physics timestep (which is wholly controllable anyway)....
I actually didn't know that. So that means that if I push timestep with FixedUpdatethat some processors on android cant keep up it will have some hick ups?
This thread gives a pretty good explanation: http://forum.unity3d.com/threads/the-truth-about-fixedupdate.231637/
Answer by emmettbebop · May 27, 2021 at 09:01 PM
This is similar to Zero's answer but has smoothing. Less code than the other smoothed examples:
private float fps = 30f;
void OnGUI()
{
float newFPS = 1.0f / Time.smoothDeltaTime;
fps = Mathf.Lerp(fps, newFPS, 0.0005f);
GUI.Label(new Rect(0, 0, 100, 100), "FPS: " + ((int)fps).ToString());
}
FYI perfect code, but, I wouldn't use smoothDeltaTime there, it won't give you what you're after; simply DeltaTime is fine
Your answer
Follow this Question
Related Questions
Physics behaviour changing with framerate - Is FixedUpdate() actually working properly? 3 Answers
iOS Keyboard makes performance suffer 3 Answers
How can I calculate the frame rate? I have some misunderstandings with deltaTime. 1 Answer
Optimizing framerate on lots of billboards 2 Answers
Frame skipped when WebRequest.Create() is called for the first time 1 Answer