Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Timer v ienumerator

Discussion in 'General Discussion' started by drewradley, Sep 26, 2014.

  1. drewradley

    drewradley

    Joined:
    Sep 22, 2010
    Posts:
    3,063
    Which is more efficient for a simple "time out"?
    A: Setting up a simple timer that subtracts by delta time
    B: An ienumerator with a yield statement.
    I always use a timer because it's what I've always done, but now I'm trying to learn efficiency in my coding rather than just using whatever I know that works.
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    timer, mainly due to garbage creation (ienumerator). If youre re-using your enumerator, not sure.

    im a fan of invoke as an alternative.
     
    calmcarrots and lmbarns like this.
  3. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    I'm a big fan of the IEnumerator because you're stating intent rather than implementation. If garbage collection is an issue, then I'd find a better way to state intent.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Coroutines are more efficient despite garbage creation; Update needs to be called via reflection which is relatively slow. However Invoke is the most efficient, but also the most limited.

    --Eric
     
  5. drewradley

    drewradley

    Joined:
    Sep 22, 2010
    Posts:
    3,063
    Invoke is a new one to me. I'll read up on it. Thanks! Every millisecond counts on mobile!
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Wouldnt the garbage degrade performance if you're doing it a lot? Creating the garbage isnt a big deal... removing it has never been free.
     
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    If you're doing it "a lot" I'd consider rolling a custom solution that fits a project's specific needs.

    How much/often do you need to use this?
    .
     
  8. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    probably redundant if you reuse your ienumerator properly.. if you were doing it alot, I would tend to use InvokeRepeating.
     
  9. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    By "a lot" I was more referring to the number of objects doing it, rather than how often an individual object repeats it.
     
  10. drewradley

    drewradley

    Joined:
    Sep 22, 2010
    Posts:
    3,063
    I have maybe 10 scripts with timers set up that I was thinking of changing to something more efficient. A couple of them have multiple timers per script. Both my player and the enemies have various timers for attacks so there can be a couple dozen in a scene, depending on how many randomly spawn. And it's for mobile if that makes a difference - which I'm sure it does.