Search Unity

Better performance: function in Update() or Coroutine with loop

Discussion in 'Scripting' started by Verdemis, Sep 28, 2018.

  1. Verdemis

    Verdemis

    Joined:
    Apr 27, 2013
    Posts:
    92
    Hello everyone,

    I have a function which has to be called repeatedly. At the moment the function gets called by the update function every frame. Now I ask myself if a Coroutine with a loop could be a better solution?

    Does anyone know if there a performance differences in those two methods.. or are there any other
    aspects which makes one way superior over the other way?

    Best,
    Verdemis
     
  2. ToshoDaimos

    ToshoDaimos

    Joined:
    Jan 30, 2013
    Posts:
    679
    With coroutine you can control the frequency of calls. If your code is doing something expensive which doesn't have to be done every frame then it's better to have it in a coroutine. You can also add timing code to Update and execute your method less frequently without a coroutine.
     
    Verdemis likes this.
  3. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    If you have 1000 enabled components with a Update method 1000 updates will be called. Even if only 2 of those will do relevant work. There is no way for Unity to optimize this. With Courtine and WaitForSeconds they can optimize so the do not need to poll those components that are sleeping.

    I almost always use Coroutines over Update. Its also easier to control the flow, since Coroutines are like small workflow engines.
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Bonus: MicroCoroutines from UniRx actually yield even better performance than default Coroutines, due to less/better memory management. Although, they're limited to Update/FixedUpdate.

    Bonus x2: UniRx's micro coroutines also managed via single dispatcher, which as well boosts the performance.


    Worth mentioning, that each Awake/Start/Update/FixedUpdate/LateUpdate is a hidden interop call. Unavoidable one even if you define empty body of the method. So, don't leave empty Unity methods.
     
    Last edited: Sep 28, 2018
  5. sas67uss

    sas67uss

    Joined:
    Feb 8, 2020
    Posts:
    81
  6. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Coroutines are bad for performance in general. You get an alloc on each on StartCoroutine, and yield if it uses new yield instruction.

    Moreover, they bind state to logic. And in case you want to do serialization, they become a major pain.

    Avoid them if you can for mobile.You can do that by running tasks as runners from single update.

    For the generic AI solution, I'd suggest Behavior Designer, it would save you a lot of time and headache. Trust me, I've debugged custom made AI multiple times, and its not fun unless you've got a proper tooling.
     
    Last edited: Oct 10, 2021
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    The state machine for your enemy behaviors is extremely unlikely to be a performance issue on any target even mobile. Have you seen otherwise in the form of data from a profiler run?

    REMEMBER: DO NOT OPTIMIZE CODE JUST BECAUSE... If you don't have a problem, DO NOT OPTIMIZE!

    If you DO have a problem, always start by using the profiler:

    Window -> Analysis -> Profiler

    Failure to use the profiler means you're just guessing, making a mess of your code for no good reason.

    https://forum.unity.com/threads/is-...ng-square-roots-in-2021.1111063/#post-7148770

    Notes on optimizing UnityEngine.UI setups:

    https://forum.unity.com/threads/how...form-data-into-an-array.1134520/#post-7289413
     
    Bunny83 likes this.
  8. sas67uss

    sas67uss

    Joined:
    Feb 8, 2020
    Posts:
    81
    Thanks for your reply
    What's you means?

    My AI is shooter and not very complex , maybe its most complex behaviors are finding appropriate covers for hiding and shooting. I'm not sure about Behavior Designer ,specially about those performances on mobile and the freedom to customize its code without spending too much time learning it.
     
  9. sas67uss

    sas67uss

    Joined:
    Feb 8, 2020
    Posts:
    81
    Thanks for your attention
    Yes, your guess is correct. I have taken precautionary measures to avoid losing performance and prevention of full CPU load , especially because of the limited battery life on the mobile platform, the idea is that any optimization, however unnecessary, can be useful.
    Although my AI system is not complicated and is just a shooter and cover, I like to have more detailed animations on enemy characters like " Call of Duty " and I know this consumes more CPU power , Therefore, I seek to implement it with the most optimal method and structure. And because Unity Job System is very new and its community is weak, I did not go for it.
     
  10. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    I use a custom update system, that runs every "Update" from single method / loop. Plus have some extension methods to insert / remove from it. Its always possible to split coroutine logic into separate step-by-step running logic "update".

    I've used it a bunch of times on mobile. Most of performance impact comes from your domain logic / nodes, not from the tree itself.

    I'd say its pretty efficient, simple to pick up, and easy to extend when neccessary.
     
    sas67uss likes this.