Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by fractalfrenzy · Jan 12, 2016 at 05:40 AM · optimizationcoroutinesframe rate

How to do the most amount of work possible per frame without lagging?

Hello,

I'm trying to design a coroutine that does a fairly expensive operation on a variable number of objects, continually. I know there typically won't be enough computational power to complete this operation on every object each frame. So I want to do the operation on SOME of the objects each frame, and cycle through them. I also want to ensure that I update the maximum number of objects each frame without causing a lag. I.e. the couroutine should not take longer than the frame rate.

I'm brainstorming ways to accomplish this. Right now I'm thinking of starting with a relatively low number of objects to update each frame, the next frame I'll check the frame rate and make sure it is acceptable, if so I'll increase the number of objects to update, and continue doing this until the frame rate decreases, and go back by one.

Does this sound like a sensible approach or is there a better way to accomplish this? Thanks.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Eno-Khaon · Jan 12, 2016 at 06:19 AM

If you don't need to complete the task per frame, here's one possible solution:

 // C#
 float timeStamp; // This will keep track of how long an operation takes.
 public float targetFrameRate = 60.0f; // Your intended framerate. Details below script.
 float maximumTimePerFrame; // Here, you'll enforce a maximum delay between frames.
 
 void Start()
 {
     maximumTimePerFrame = 1.0f / targetFrameRate;
     StartCoroutine(MyOperation());
 }
 
 IEnumerator MyOperation()
 {
     timeStamp = Time.realtimeSinceStartup;
     while(**DOING STUFF**) // i.e. if intended for non-stop calculation, while(true) would even do the trick
     {
         // do stuff here
         if(Time.realtimeSinceStartup > timeStamp + maximumTimePerFrame)
         {
             yield return null(); // wait for next frame of gameplay
             timeStamp = Time.realtimeSinceStartup;
         }
     }
 }

This will give you a fundamental performance monitor to ensure a framerate above a specified threshhold while performing calculations in a virtually endless loop (or endless, depending on implementation). The key element preventing the endless loop from freezing Unity is time monitoring paired with yield in the coroutine.

That said, specifying a framerate manually is probably not a good idea: If performance is already bad for a player, then this offloaded task will function extremely slowly, if there's any urgency in its completion.

Because of this, it may be prudent to monitor average frame rate and throttle the target frame rate relative to that (ensuring that the target doesn't continuously drop due to the calculations performed taxing the hardware).

That said, another option, especially for purely aesthetic calculations and tasks, would be multithreading (see this answer by @Bunny83 for a reasonable implementation), but do be aware that Unity and multithreading don't get along well. If additional threads aren't properly monitored, you can do wonderful things to your computer.

As an example of an aesthetic task, I've created separate thread(s) for processing vertex colors in conjunction with a shader to apply ripples to water surfaces. By putting it in a separate thread, tens of thousands of calculations were saved per frame for something that didn't have any need to negatively impact performance.

Anyway, to really answer your question, coroutines with time monitoring are a fairly easy solution to implement and will be sufficient in a majority of circumstances. The more important the calculations being performed, the more carefully you'll want to monitor how many are processed per frame.

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

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

Related Questions

Unity frame rates drop to 1.2 1 Answer

Why do my Coroutines allocate memory when they execute? 1 Answer

What is the fastest way to work a coroutine before yielding to maintain 60 FPS? 1 Answer

Why does this script lag so much? 2 Answers

Does anyone know why continuously saving to playerprefs would cause a performance drop ? 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