Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
15
Question by ina · May 13, 2011 at 10:31 AM · fpsoptimizationframerate

Accurate Frames Per Second Count

What's the best or most accurate way to get a FPS count? Should the frameCount and Time.time division be put in Update or LateUpdate, or does it matter?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
22
Best Answer

Answer by Bunny83 · May 13, 2011 at 11:11 AM

Update or LateUpdate doesn't matter. The question is what do you mean by most accurate. frames per second is never a constant value, it can fluctuate even within one second.

 var fps = 1.0/Time.deltaTime;


that gives you the current FPS but that will change every frame.

You could smooth the delta, but it will be less accurate.

 var deltaTime = 0.0;
 var fps = 0.0;
 
 function Update()
 {
     deltaTime += Time.deltaTime;
     deltaTime /= 2.0;
     fps = 1.0/deltaTime;
 }

If you want to display the value, the best way is to accumulate the frames yourself and calculate the FPS in fix time steps.

 var frameCount = 0;
 var nextUpdate = 0.0;
 var fps = 0.0;
 var updateRate = 4.0;  // 4 updates per sec.
 
 function Start()
 {
     nextUpdate = Time.time;
 }
 
 function Update()
 {
     frameCount++;
     if (Time.time > nextUpdate)
     {
         nextUpdate += 1.0/updateRate;
         fps = frameCount * updateRate;
         frameCount = 0;
     }
 }

edit
Added a little bit more accuracy to the second solution (like mentioned in the comments) and here's another one that's probably the "most" accurate.

 var frameCount = 0;
 var dt = 0.0;
 var fps = 0.0;
 var updateRate = 4.0;  // 4 updates per sec.
 
 function Update()
 {
     frameCount++;
     dt += Time.deltaTime;
     if (dt > 1.0/updateRate)
     {
         fps = frameCount / dt ;
         frameCount = 0;
         dt -= 1.0/updateRate;
     }
 }
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image a1studmuffin · Dec 14, 2013 at 11:27 PM 2
Share

A $$anonymous$$or bugfix to the last solution above. To ensure you don't lose 1 frame of precision on each update tick, replace:

nextUpdate = Time.time + 1.0/updateRate;

with:

nextUpdate += 1.0/updateRate;

avatar image Bunny83 · Dec 15, 2013 at 10:57 PM 0
Share

@a1studmuffin:
Yes, good spot. I usually do it that way ;)

Well if you do this you should initialize nextUpdate in Start with Time.time. Or if you (for some reason) have disabled the script in the beginning and enable it after lets say 10$$anonymous$$ while in the game it would take 2400 frames (about 40 seconds @ 60 fps) until the correct value is shown and nextUpdate would catch up with the current time. That's why you have to be careful with pure incremental values ;)

Looking at this now it's actually not very precise ;) Of course it shows you the actual frame count within this time period but doesn't take fractions into account. So the number will always be a whole number. It's probably better to accumulate deltaTime over a certain timespan and calculate the FPS from the dt value.

I'll fix the second add a third solution ;)

avatar image CesarNascimento · Mar 09, 2016 at 01:06 PM 0
Share

After all this time I still don't know why

   dt -= 1.0/updateRate;

isn't

   dt = 0;

avatar image weatxyz CesarNascimento · Mar 22, 2017 at 03:25 PM 1
Share

He's using this:

 dt -= 1.0/updateRate

...so he doesn't lose the fractional amount of time that has past. For instance, dt could be anything higher than 0.25, in his example:

 if (dt > 1.0/updateRate)

If dt was say, 0.35, then it's 0.1 over 0.25, and he doesn't want to lose that accuracy every update.

This method will fail in cases where the frame lasts 0.5 or more seconds longer per frame, but at that point you know you have some performance issues so the counter is still doing its job :D.

avatar image nt314p · Aug 06, 2017 at 11:21 PM 0
Share

The framerate is lower than the built in stats, anybody else finding this? Also which one is more accurate?

avatar image vampir26 · Aug 05, 2018 at 11:55 AM 1
Share

I recommend to use unscaledDeltaTime or it will not show the FPS while pause menu.

Here is my final script, just add it in your scene, add a Text reference and you FPS is displayed formatted.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 /// <summary>
 /// This script calculate the current fps and show it to a text ui.
 /// </summary>
 public class UiDisplayFps : $$anonymous$$onoBehaviour
 {
     public string formatedString = "{value} FPS";
     public Text txtFps;
 
     public float updateRateSeconds = 4.0F;
 
     int frameCount = 0;
     float dt = 0.0F;
     float fps = 0.0F;
 
     void Update()
     {
         frameCount++;
         dt += Time.unscaledDeltaTime;
         if (dt > 1.0 / updateRateSeconds)
         {
             fps = frameCount / dt;
             frameCount = 0;
             dt -= 1.0F / updateRateSeconds;
         }
         txtFps.text = formatedString.Replace("{value}", System.$$anonymous$$ath.Round(fps, 1).ToString("0.0"));
     }
 }
Show more comments
avatar image
3

Answer by CHPedersen · May 13, 2011 at 11:05 AM

Do you mean after the project has been built? If you want to just see the FPS, you can click the "Stats"-button while the program runs in the editor. It displays the FPS.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ina · May 13, 2011 at 06:16 PM 0
Share

That does not work in mobile though..

avatar image dentedpixel · Mar 04, 2013 at 01:38 PM 1
Share

FPS Graph works on mobile, it will give you the frame rate and other performance indicators. The framerate in the stats window is not always reliable as explained on this forum page.

FPS Graph

avatar image
0

Answer by gabearts · Apr 21, 2020 at 10:02 PM

That was an awesome insight and wanted to share my own version, based off the same thing but another way to approach it with refresh interval.

  public class FPSCounter : MonoBehaviour {
 
     public float timer, refresh, avgFramerate;
     string display = "{0} FPS";
     private Text m_Text;
 
     private void Start()
     {
         m_Text = GetComponent<Text>();
     }
 
 
     private void Update()
     {
         float timelapse = Time.smoothDeltaTime;
         timer = timer <= 0 ? refresh : timer -= timelapse;
 
         if(timer <= 0) avgFramerate = (int) (1f / timelapse);
         m_Text.text = string.Format(display,avgFramerate.ToString());
     } 
 }






Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Desertleaf · Mar 19, 2021 at 03:45 PM

None of these are counting frames but rather a float value within the time spent during the update.

These solutions are completely false and made up.

The correct solution would be to look into .Net API and getting the GPU’s actual frame completion. Once the frame is completed, you compare that to the time spent.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by DarkGate · Jul 03, 2021 at 09:49 AM

Bit late to the party, below is the solution provided in the Unity's Standard Assets.

 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);
       }
     }
   }
 }
 

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Framerate drops and Camera.render is high -1 Answers

Framerate drops when I preview my game, what is particleSystem.renderSingle in profiler? 0 Answers

Android lag 1 Answer

How to render severals cameras at differents Frame Rate ? 2 Answers

Why CPU waits for GPU when there is not much work on GPU? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges