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
1
Question by michael-v · Jan 20, 2014 at 12:29 PM · fpsrenderer

Calculate time per frame

Hello, i'm looking for a way to calculate time per frame. It's not FPS which is just number of Updates per second, it time it took to render this single frame. Time per frame should be calculated in ms not in fps, so 30fps is ~= 33ms per frame per second.

So simply i want to track when main loop started to render this particular frame and when it's done.

EDIT: I should clarify that i want to calculate time per frame independent of targetFps. Otherwise deltaTime would suffice. But when targetFps is set, for ex., to 30fps, deltaTime will always be around 0.033sec, when in reality frame could be rendered much faster. I want this stat to provide myself with insight just by how much my targetFps is lower than it could be.

EDIT2: As of right now i see i can't get it. What i want in nutshell (meta-c).

 int last_start = 0, last_done = 0; 
 time_t last_frame_ms;

 loop 
 { 
     int last_start = gettime(); 
     UNITY_LOOP(last_frame_ms: last_frame_ms); 
     last_done = gettime(); 
     last_frame_ms = last_start - last_done; 
 }
Comment
Add comment · Show 4
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 GameVortex · Jan 20, 2014 at 12:30 PM 0
Share

So you want to calculate **Time.deltaTime** manually?

avatar image michael-v · Jan 20, 2014 at 01:01 PM 0
Share

Not exactly, Time.deltaTime calculated with per frame time padding when targetFps is set. I want to calculate exaclty how long this frame was rendered without any delays due to targetFps

avatar image VildNinja · Jan 20, 2014 at 01:06 PM 0
Share

This sounds like something the profiler does for you. Do you need it in game, or just for testing purposes?

avatar image michael-v · Jan 20, 2014 at 01:09 PM 0
Share

I want it in game. I would like to send stats to my server to then decide on targetFps value. Also it will give me stat about just how fast my game is perfo$$anonymous$$g after each update.

I may have 30 fps on 90% of users when in fact frames being rendered in 0.016, and i can up my targetFps to 60 without worry.

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by moemartin2 · Jan 21, 2014 at 07:46 AM

You can use fps =1/Time.deltaTime.

Comment
Add comment · Show 1 · 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 michael-v · Jan 21, 2014 at 08:10 AM 0
Share

deltaTime is targetFps dependant. For ex.: if you are to set targetFps to 30, than you will NEVER get deltaTime less than 0.033, you may get more if frame rate is dropping less than 30, but you'll never ever get more.

$$anonymous$$y goal is to calculate how many milliseconds was the current/last frame rendered, not time between main loop invoking Update function.

As of right now i see i can't get it. What i want in nutshell (meta-c).

int last_start = 0, last_done = 0; time_t last_frame_ms;

loop { int last_start = gettime(); UNITY_RENDERER(last_frame_ms: last_frame_ms); last_done = gettime(); last_frame_ms = last_start - last_done; }

avatar image
0
Wiki

Answer by brycedaawg · Jan 20, 2014 at 03:31 PM

Time.realTimeSinceStartup may be what you're after. Time.realTimeSinceStartup gives you the exact time (in seconds) since your game was started. It ignores timescale and fixed framerate and comes directly from your system's clock. This means that if you were log Time.realTimeSinceStartup at the start of your Update method and then log it again at the end, you'd receive two different values because Time.realTimeSinceStartup is (like the name suggests) in real time.

I'm unsure if Update is called more than once per frame if you limit your framerate, but if it is all you'd need to do is store Time.realTimeSinceStartup in a global every Update cycle like so:

 public float timeLastFrame;
 
 void Start()
 {
     //Initialize our timeLastFrame variable
     timeLastFrame = Time.realTimeSinceStartup;
 }
 
 void Update()
 {
     float realDeltaTime = Time.realTimeSinceStartup - timeLastFrame;
     TimeLastFrame = Time.realTimeSinceStartup;
 
     //Do whattever you want with delta time here...
 }
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 michael-v · Jan 21, 2014 at 06:47 AM 0
Share

Regrettably Update will be invoke only once per frame, so not faster than target framerate. It is right place to check how many frames actually rendered per second (how many updates was invoked and when), but you can't check how long did the previous frame has been rendered.

I tried to experiment with OnPreRender/OnPostRender hooks but they give some very strange results.

avatar image brycedaawg · Jan 21, 2014 at 03:27 PM 0
Share

Ah darn it. Well I looked over the $$anonymous$$onobehaviour documentation and I don't believe there's any method that is invoked independently of the engine's fixed frame-rate. I could be wrong about this so good luck to you if you keep looking.

avatar image
0

Answer by hoihoi87 · Aug 15, 2018 at 09:03 PM

This doesn't really solve the original problem but it is a work around. If you determine your min spec device of the majority of your users (I'm guessing you are collecting device info), you could run the game with uninhibited FPS as a local test, then use those results to set your FPS for the release version.

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 RShields · Aug 15, 2018 at 09:54 PM

With newer versions of Unity, you can go to Window > Analysis > Profiler and check all the times in ms.

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 hoihoi87 · Aug 16, 2018 at 01:17 AM 0
Share

Any way to get this data from inside the game? I and the original poster are using the data for telemetry of users.

avatar image RShields hoihoi87 · Aug 16, 2018 at 02:45 PM 0
Share

You can definitely save the data, if that's what you're trying to do: https://stackoverflow.com/questions/32809888/how-can-i-save-unity-statistics-or-unity-profiler-statistics-stats-on-cpu-rend

I'd be very surprised if you couldn't extract the data to the game in a similar way

avatar image
0

Answer by SupriyaRaul · Feb 18, 2020 at 02:11 AM

Did you find a solution for this problem @genius-fx ?? I also want to find the render time for every frame. I was thinking about using OnPreRender and OnPostRender, but you said you got weird results with it!?

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

25 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 avatar image 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

Changing two different objects renderer colour 1 Answer

Update Renderer Bounding Volumes - Lower Game Performance 1 Answer

Always render a object on top of another. 8 Answers

Render Transparent Geometry takes high CPU and low fps 1 Answer

Option to Toggle Renderers/Colliders OFF Slows Game FPS to Standstill 1 Answer


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