Search Unity

Coroutines performance question

Discussion in 'Windows' started by tyomklz, Apr 15, 2017.

  1. tyomklz

    tyomklz

    Joined:
    Jul 31, 2015
    Posts:
    48
    Is running coroutines in separate scripts faster than running same coroutines in a single script? Say, there are 20 coroutines with http requests, for example. Would separation improve it or not?
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
  3. tyomklz

    tyomklz

    Joined:
    Jul 31, 2015
    Posts:
    48
    Thank you for the answer. So, like, each time I do StartCoroutine() it's a separate thread just for the exception that I can stop all coroutines of the used script at once. Right?
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    They aren't threads at all. Coroutines are performed on the main thread, You can consider them just a convenience really.
     
  5. tyomklz

    tyomklz

    Joined:
    Jul 31, 2015
    Posts:
    48
    You've opened my eyes to the problem now, thanks a lot, hippocoder. The thread may now be closed (heh, thread).
     
    iancox890 likes this.
  6. UziMonkey

    UziMonkey

    Joined:
    Nov 7, 2012
    Posts:
    206
    Coroutines are not threads. The way I like to think about coroutines is each coroutine is like another Update method. It's called in the same way as part of the update loop by Unity. You can yield different things to tell Unity when to call the coroutine again, like a WaitForSeconds object or WaitForFixedUpdate. Once you understand that they're pretty easy to use.
     
    Bunny83 likes this.
  7. tyomklz

    tyomklz

    Joined:
    Jul 31, 2015
    Posts:
    48
    Thank you for the answer, UziMonkey. I know how to use coroutines, though I didn't know what coroutines exactly are. I already did a research about it and also made a test script with Update() vs coroutine vs thread (using C# classes), the result was that coroutine turned out to be basically the same as Update(), just parallel, and the thread was wa-a-a-a-ay beyond both of them (is what I've been looking for). Bottom line is that I thought coroutines are actually what C# thread is, but I was so wrong :(
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Some of Unity API designed for coroutines is threaded, but the coroutine itself is not a thread, that is when execution comes to your script, it not on separate thread and is just like another Update().
    In example UnityWebRequest is executed on a separate thread and when it completes, it triggers coroutine to continue execution next time all Update() are executed.

    As for original question: it is completely irrelevant if coroutines were started from single or multiple scripts, the performance characteristics will be identical.
     
  9. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,822
    This is not a thread, this is a co-routine :rolleyes:
     
  10. beniciodomi

    beniciodomi

    Joined:
    Jul 2, 2020
    Posts:
    2
    Using coroutines have a perfmorance impact??
     
  11. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    Writing code has performance impact in general :D. The question is usually how big that impact is.
     
    Bunny83 likes this.
  12. sas67uss

    sas67uss

    Joined:
    Feb 8, 2020
    Posts:
    81