Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Hypothetical Question

Discussion in 'Scripting' started by BenZed, Dec 18, 2014.

  1. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Lets say I need to keep track of a million timer countdowns: A float variable which decrements by Time.deltaTime until it reaches zero, and then some function gets called. Which would theoretically perform better:

    One timer being tracked in the Update call of a MonoBehaviour on a million game objects, or one game object looping through a million timers in one update call?

    On the surface it seems like a silly question, because of course a million game objects is going to have a lot of data not-relevant to timer float variable. I'm imagining that unity wont put all update calls on the same thread, so maybe splitting up tasks similar to this across game objects would be better than having one gargantuan update that could chug and skip frames. Anyone with a strong understand of unity's internals have any insight?
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    I would:
    One timer, and a sorted array of float times for when events should expire
    If the 0th element is < timer, remove from the array (or increase index), and trigger event
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    OK, so you're talking about a lot of timers. So I would want to avoid looping over every entry and decrementing them, and especially don't want to have a million calls to 'Update' just for a little timer. That'll just take forever.

    Instead how about we precalculate the end time for each entry, store as that, and then we just have to compare if the current time is greater than the end time to know if it's done. Better, lets order them in the array/list in ascending order. This way if the first entry in the list is not done, then all following ones aren't done either, so we can stop right there.

    I went ahead and implemented this here:
    https://code.google.com/p/spacepupp...owse/trunk/SpacepuppyBase/Timers/BulkTimer.cs

    Note I implement an interface called ITimer. This just allows injecting the Timer into a manager that auto-ticks (calls update) on timers. Like this one here:
    https://code.google.com/p/spacepupp...wse/trunk/SpacepuppyBase/Timers/GameTimers.cs

    You can just forego the ITimer and manually call 'Update' on whatever Update interval you want.

    The 'StopWhenEmpty' property is there because in my ITimer setup, completed timers are thrown out. Because this BulkTimer really is a bunch of timers grouped together, I'm allowing it to consider itself 'complete' once all timers have finished. But you might want to leave the empty timer in there for whatever reason so you can add more later. Again, you can ignore this if you want.
     
    Last edited: Dec 18, 2014
    TonyLi likes this.
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Door #2. Calling Update has overhead, so multiply that overhead X 1 million.

    --Eric
     
  5. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Thanks for the responses everyone!