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
0
Question by JasonB · Nov 18, 2012 at 01:31 AM · performancecoroutineoptimizationframe

Does a coroutine do all its work during one frame?

Sounds like a yes or no question, but I did want to extrapolate. It's more of an optimization question.

If yes, what would be a good way to spread calculations out across a few different frames to reduce performance spiking?

Should I place a `yield return new WaitForEndOfFrame();` between calculations? Does this do what I want? Is there another more proper way?

The reason I ask is, while I'm not doing anything particularly heavy, I do have a lot happening that I think is currently all being calculated in one frame, leading to uneven performance.

Example:

Will this,

 IEnumerator Test_Coroutine()
 {
   var1 += 1;
   yield return new WaitForEndOfFrame();
   var2 += 1;
   yield return new WaitForEndOfFrame();
   var3 += 1;
   yield return new WaitForEndOfFrame();
   var4 += 1;
 }

...result in smoother performance than just running all 4 calculations in the same frame, or is this an incorrect use of WaitForEndOfFrame? (For the sake of the example, I'm well aware that adding 1 to a variable isn't a hard calculation. Pretend I'm doing something more advanced in place of these. :))

Thanks for any help. It's really just a clarification that I need, and hopefully it'll help anyone else with similar questions.

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
Best Answer

Answer by aldonaletto · Nov 18, 2012 at 02:07 AM

You could simply use yield return null instead: the coroutine stops at this instruction and resumes next frame in the following line. WaitForEndOfFrame does the same, but only after all cameras and GUI has been rendered - useful when getting screen snapshots.
A coroutine is a good way to span infrequent and time consuming pieces of code (usually loops) across several frames in order to reduce performance hiccups, as you already knew. But a compromise must be found: yielding after each iteration in a long loop will make the whole task take too much time. If you must create 1000 objects, for instance, yielding after each Instantiate would take 1000 frames - 33 seconds at 30 fps! If you yielded after each 10 objects, on the other hand, it would take only 3.3 seconds, and so on.

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

10 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

Related Questions

Performance issues with mesh processing 1 Answer

Should large amounts of static objects outside of the camera’s clipping planes range be lagging my iOS game? 1 Answer

How to fix duplication of materials with blender? 0 Answers

No me muestra el cuello de botella en Unity Profiler 1 Answer

UI-based game memory and performance issue. 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