Search Unity

IEnumerator or Update?

Discussion in 'Scripting' started by Shayke, Jun 15, 2018.

  1. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    Hey, i read somewhere that to use the update function to count time is better than using IEnumerators.
    What i care about is just the performance.
    So my question is what better for memory/GC/performance or whatever.

    Lets say i have a player that between attacks needs to rest.
    So i can do it with the IEnum by reducing the Counter or i can put it in update like that:

    Code (CSharp):
    1.  
    2. void Update()
    3. {
    4. if(Counter > 0)
    5. {
    6. Counter -= Time.DeltaTime;
    7. }
    8. }
    So i can't choose how to program those things.
    On the one hand the update function happend all time and if i have alot of counters so i can decrease my performance, and on the other hand the IEnumerators take more memory(Not sure about that).
    So, someone can explain please? :)
     
  2. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Why?

    This is certainly micro-optimisations and what should be more important is the readability of the code to you.
     
    ihgyug likes this.
  3. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    1.I care about optimization only because i want to avoid those things when i start a new project.
    2.So the asnwer is the IEnumerator worse than update counters?
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,789
    It depends.

    I don't like IEnumerators, they generate more problems than they solve.

    What you are describing is most probably never going to be a bottleneck. If it ever does, you could refactor it.
     
    Joe-Censored likes this.
  5. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    My favorite functions are IEnumerators, help me alot and i don't find it troublemaker.
    And what i ment about performance is not a single moment that full of functions, i talk about the long term playing, about losing memory with all those commands.
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you're concerned about this level of optimization, I'd expect this might perform slightly faster since you would only be doing a float comparison every update instead of a float/integer conversion comparison (you're comparing the counter float to the integer 0, and I'm pretty sure the integer 0 has to be converted to float as part of the comparison) followed by a float subtraction operation. You're probably want to do some kind of benchmarking though if you're really concerned about this kind of stuff. I haven't benchmarked it, its just my guess this would be better (by a trivial amount).

    Code (csharp):
    1.  
    2. private float counter = 0f;
    3.  
    4. //specify timeInFuture as the number of seconds in the future you want to count down
    5. private void setCounter(float timeInFuture)
    6. {
    7.     counter = Time.time + timeInFuture;
    8. }
    9.  
    10. void Update()
    11. {
    12.     if (counter > Time.time)
    13.     {
    14.         //Not done yet
    15.     }
    16.     else
    17.     {
    18.         //Done
    19.     }
    20. }
    21.  
    22.  
    As @AcidArrow mentioned, I'm also in the camp of avoiding coroutines when they aren't necessary. I generally only use them when tracking something complex in Update over time would not be very clean. For example, I use them when I need to queue up a large number of messages to several servers when my server cluster starts up, to set their starting game state, but I need to regulate how fast I send them to not overwhelm the networking API. It is a lot cleaner to track each queue for each server I'm sending to separately in a coroutine than to create a single monster queue for all servers I'm sending to that I manage in Update, for all different types of messages I'd need to send.

    But for something like a simple counter, I'd just do it in Update unless you end up with some long spaghetti code Update method for whatever reason. YMMV
     
    Last edited: Jun 15, 2018
    AcidArrow likes this.
  7. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    I love coroutines, they are great for mini workflows that exist inside your domain.

    I tend you use them when user input is mixed into the logic, it's a nice way to await asynchronous actions in a synchronous manner
     
    Joe-Censored likes this.