- Home /
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());
}
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
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.
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).
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220613184747im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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