# Coroutine take extremely long time for simply calculation

**URL:** https://discussions.unity.com/t/coroutine-take-extremely-long-time-for-simply-calculation/647130
**Category:** Scripting
**Tags:** Scripting
**Created:** [November 27, 2016, 12:54pm UTC](https://discussions.unity.com/t/coroutine-take-extremely-long-time-for-simply-calculation/647130 "2016-11-27T12:54:06Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Pontik](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@Pontik](https://discussions.unity.com/u/Pontik)
#### Post date: [November 27, 2016, 12:54pm UTC](https://discussions.unity.com/t/coroutine-take-extremely-long-time-for-simply-calculation/647130/1 "2016-11-27T12:54:06Z")

</div>

Hello community,  
i have created here some code for a simply test for something like a asynchron calculation. But i was realy surprised to see how much time a coroutine take to count to 1000? Nearly 17 seconds?! A simply while lop will take much fewer as 1ms for that. May be i do something wrong because i can not belive it is normal time for this operation.

Here is my code:

```csharp
using UnityEngine;
using System.Linq;
using System.Diagnostics;
using System.Collections;

public class UnitTest : MonoBehaviour {
    delegate void OnCalculationResult(int result);
    void Start()
    {
        DoSomeStuff();
    }

    private void DoSomeStuff()
    {
        print("I do some stuff");
        //Calculate(100, HandleResult);
        StartCoroutine(Calculate(HandleResult));
        print("I do some another stuff [1]");
        print("I do some another stuff [2]");
        print("I do some another stuff [3]");
        print("I do some another stuff [x]");
    }

    private void HandleResult(int result)
    {
        print("Calculation result handled: " + result.ToString());
    }
    private IEnumerator Calculate(OnCalculationResult ResultHandleFunction)
    {
        Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();

        var initValue = 0;
        while (initValue < 1000)
        {
            initValue++;
            yield return null;
        }
        watch.Stop();
        print("MyCoroutine is now finished in: " + watch.ElapsedMilliseconds.ToString() + " ms.");
        ResultHandleFunction(initValue);
    }
}

```

The result is: [http://shot.qip.ru/00NDav-2oHGounG8/](http://shot.qip.ru/00NDav-2oHGounG8/)

---

<div class="post-metadata">

### Author: ![L-Tyrosine](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/l-tyrosine/32/480923_2.png) [@L-Tyrosine](https://discussions.unity.com/u/L-Tyrosine)
#### Post date: [November 27, 2016, 1:03pm UTC](https://discussions.unity.com/t/coroutine-take-extremely-long-time-for-simply-calculation/647130/2 "2016-11-27T13:03:50Z")

</div>

> [@Pontik](#):
>
> i have created here some code for a simply test for something like a asynchron calculation

Coroutines does not process things [**_asynchronous_**](https://www.google.com.br/search?q=asynchronous&spell=1&sa=X&ved=0ahUKEwi__obt_8jQAhUFxpAKHYQKCesQvwUIGSgA) (like a thread would do), they just span the process over frames (or time if you use WaitForSeconds).

In your case, each iteration has to wait 1 frame

```auto
yield return null;

```

So this time that you see it is the time that 1000 frames take in your game.

---

<div class="post-metadata">

### Author: ![Pontik](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@Pontik](https://discussions.unity.com/u/Pontik)
#### Post date: [November 27, 2016, 1:21pm UTC](https://discussions.unity.com/t/coroutine-take-extremely-long-time-for-simply-calculation/647130/3 "2016-11-27T13:21:58Z")

</div>

Oh, now i see! Thank you. Now it make sense for me!

---

<div class="post-metadata">

### Author: ![ericbegue](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/ericbegue/32/426230_2.png) [@ericbegue](https://discussions.unity.com/u/ericbegue)
#### Post date: [November 27, 2016, 1:34pm UTC](https://discussions.unity.com/t/coroutine-take-extremely-long-time-for-simply-calculation/647130/4 "2016-11-27T13:34:34Z")

</div>

coroutine **!=** thread

---

<div class="post-metadata">

### Author: ![Pontik](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@Pontik](https://discussions.unity.com/u/Pontik)
#### Post date: [November 27, 2016, 1:41pm UTC](https://discussions.unity.com/t/coroutine-take-extremely-long-time-for-simply-calculation/647130/5 "2016-11-27T13:41:36Z")

</div>

coroutine **!=** thread

yes, i understand. The sense of this test was **not** to make a real asyncronous operation, but the code should go further and not wait for calculation end. My keyword asynchron is wrong in this case, i know.

---

<div class="post-metadata">

### Author: ![Pontik](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@Pontik](https://discussions.unity.com/u/Pontik)
#### Post date: [November 27, 2016, 2:17pm UTC](https://discussions.unity.com/t/coroutine-take-extremely-long-time-for-simply-calculation/647130/6 "2016-11-27T14:17:54Z")

</div>

My mistake was to put yield method inside the while loop. It should be on the first line inside Calculate IEnumerator. In this case it return one time wait for 1 frame and the code below will execute “callback” after it is completed. In this time the parent function where i started my coroutine can go further without waiting for a result.
