Search Unity

Is there performance limitation to considere with monobehavior's update ?

Discussion in 'Scripting' started by anthonov, Jan 9, 2022.

  1. anthonov

    anthonov

    Joined:
    Sep 24, 2015
    Posts:
    160
    What I mean with a simple function over one frame ;

    1 : running this function each update over 100 gameobject,
    VS
    2 : running 100 time this function in the same update in one gameobject.

    So my question is, is there a performance cost from jumping between updates to do the same job that could be done bulk in one update ?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    Not likely anything of consequence given that the data for GameObject and its components are all over the place anyway in memory.

    That said, there is a small overhead for the engine calling Update.

    The only real answer to this is to create a simple test project that does one or the other (doing some test work), spin up the profiler and see if there's any measurable difference.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Why do that, when somebody working at Unity already did it and wrote a blog post about it.

    It's pretty old, 7 years by now! But the project is on GitHub. I updated and automated it a bit, and the results on my computer, on a build, using Mono:

    10000 Update() calls gives a frame time of ~1.47 milliseconds
    10000 calls from the manager script gives a frame time of ~0.06 milliseconds
    10000 Coroutines updating with yield return null gives a frame time of ~5.45 milliseconds

    So the difference on my computer is that on a very contrived example, Unity calling Update takes about 25 times longer than having an Update manager doing it.

    I tried using IL2CPP instead of Mono, but IL2CPP isn't distributed with it's required dependencies and right now I can't be assed to install a bunch of random Visual Studio/C++ stuff on this computer until it works.
     
    _geo__ likes this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    Agreed. And to be clear, doing this 100 times really isn't going to make a difference unless it's just that; 10000 times however is another matter. Doing something 100 times can soon end up doing that 100 times so before you know it, 10000 times. ;)

    Doing stuff in bulk is always going to be better, especially if you can avoid cache misses however it's balanced against if you need such optimization and any potential code complexities.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Also, to note, if you want to do a manager-like thing, but still stay in MonoBehaviour (ie. you're sane), I recommend using the PlayerLoop and create a system instead of having a MonoBehaviour manager. The MonoBehaviour manager has a lifetime tied to the scene, and has a lot of other things attached to it that you don't really care about. PlayerLoop allows you to just have a static method that gets called every frame. You can even decide where it executes in relation to Update/LateUpdate.
     
    _geo__ and MelvMay like this.
  6. anthonov

    anthonov

    Joined:
    Sep 24, 2015
    Posts:
    160
    Thanks this is very helpfull