1
\$\begingroup\$

So I wrote my own script for calculating framerate but it consistently comes out with a different value than Unitys idea of current framerate.

This is my implementation

  void UpdateFPSStats()
{
double frameTime = new double();
frameTime = Time.deltaTime;
m_times[frameIndex] = frameTime;

frameIndex++;

if (frameIndex == m_numberFrames)
{
  frameIndex = 0;
}

int numberSamples = 0;
double averageFrameTime = 0.0f;
foreach(double t in m_times)
{
  numberSamples++;

  averageFrameTime += t;
}

if (numberSamples > 0)
{
  averageFrameTime = averageFrameTime / numberSamples;
  m_frameRate = 1 / averageFrameTime;
}
}

where m_frames is a list of the last 60 frames frame times and UpdateFPSStats is called in Unitys Update(dt) function.

On average in my scene my fps calculation gives a steady 50fps, while Unity calculates a steady 80fps.

Which is right and why?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Unity's built-in editor framerate estimates the performance without the editor overhead. If you go into profiler, editor overhead likely accounts for around 40% of your games processing, if the difference is 80fps to 50fps.

Your method is an accurate recording of what your actual FPS is, while Unity is trying to give you an estimation of what the FPS would be in a build.

Also, you should also look into Time.unscaledDeltaTime or Time.smoothDeltaTime to get around some of the reasons that your method might return an incorrect value.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Thanks for the answer. Is that documented anywhere (that the unity fps is trying to account for the editor/debug overhead)? \$\endgroup\$
    – unknownSPY
    Nov 30, 2018 at 19:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .