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 Luiz_Thiago · Mar 02, 2015 at 12:50 PM · coroutineupdate

What is the best approach? Coroutine or calculate the "Elapsed Time" in the Update?

Hello everybody.

For actions that will occur within a time limit, and loop, which would be the best approach in performance for mobile?

 void Update () 
 {
     _elapsedConsumeTime += Time.deltaTime;
     if (_elapsedConsumeTime >= 15)
     {
         _elapsedConsumeTime = 0;
         //DO SOMETHING...
     }
 }

or

 IEnumerator DoSomething () 
 {
     yield return new WaitForSeconds(15f);
 
     //DO SOMETHING
 
     StartCoroutine(DoSomething());
 }
Comment
Add comment · Show 1
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 ThomasVandenberghe · Mar 02, 2015 at 01:05 PM 0
Share

I don't think it would matter very much, and for this specific case (looping and no parameters), I'd even go for the InvokeRepeating function

3 Replies

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

Answer by fafase · Mar 02, 2015 at 01:18 PM

Here is why I would use the coroutine:

  • it only performs when needed, the update would require an if statement (the performance gain is more than useless though).

  • it keeps your update clean from a whole bunch of if's here and there.

  • it makes your code specific to what it should do.

  • The allow closure of variables. In the update you would need to keep track of elapsed time using a global variable, while the coroutine would create the variable within and get rid of it on leave.

    IEnumerator Timer(){ float timer = 0f; while(timer < 5){ timer += Time.deltaTime; yield return null; } // here timer is removed from memory }

Same with Update

 float timer = 0;
 void Update(){
     if(timer < 5){
          timer += Time.deltaTime; 
          return;
     }
     // timer is still in memory but useless (ok...it is 4 bytes only...)
 }

now with cons:

  • Coroutine creates new object (IEnumerator) so if you start a lot of coroutine, you create a lot of objects, tiny ones...

  • Coroutines tend to make things more complex to follow, you may think a variable has a certain value but a coroutine is changing it and you get lost...a state machine fixes that though.

  • Coroutines do not allow to use ref parameter so it gets slightly more complex to modify a value within a coroutine and use it elsewhere, though again it is possible with some delegates.

All in all, I would use coroutines since the last two cons can be fixed with some experience.

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
3

Answer by DiegoSLTS · Mar 02, 2015 at 01:23 PM

The update method is probably faster since coroutines need more stuff happening on the background to work, but it's probably such a small difference that it won't matter. In fact, the "//DO SOMETHING" part would be code that might actually affect performance in a significant way, and it would be the same with any method.

You probably have other stuffs that can be optimized before getting as low level as this.

So, I'd forget about performance to make the answer and go for readability, maintenance and flexibility. The second method is far better, you clearly state that you're doing something that should be delayed 15 seconds (readability), you don't have to read what the loop does and you can't as much errors as the other method (maintenance). Also, with coroutines you can start multiple coroutines, with the update method you'll be adding a lot of "elapsedTime" variables to keep track of everything (flexibility).

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 Luiz_Thiago · Mar 02, 2015 at 01:48 PM

Guys! Ty! I loved all the answers, and I mark all as correct if it were possible! Thank you for sharing your knowledge with me! I will take all this into consideration!

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

21 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

Related Questions

How to slow down large code? 1 Answer

void update working under conditions 1 Answer

Coroutine in Extension Method 1 Answer

WebGL prevent Unresponsive script warning 1 Answer

Load Level when dead for 1 second 2 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