x


How expensive are coroutines on iPhone?

I've recently discovered coroutines, which make a sequence of events, separated by time for instance, easy to do, or allows me to delay a bit of script logic very easily - i.e. play the animation 5 seconds from now. I'm developing for iOS however, so I'm wondering whether coroutines are a good idea on iOS or are they likely to quickly impact performance?

I really don't want to have to start writing lots of state machines in the Update() method - are coroutines really just handling that boilerplate for me? If I yield return StartCoroutine() am I creating some heavy weight object? What about returning new WaitForSeconds() objects?

I appreciate this might be quite a tough question to answer 'off the top of your head', so I guess I am looking for; either someone who has some metrics; someone 'in the know'; or perhaps someone writing for iOS who is using them and has had either a good or bad experience as a result and is willing to share! :)

Thanks in advance H

more ▼

asked Jul 31 '11 at 09:39 PM

Bovine gravatar image

Bovine
1.6k 26 30 47

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Coroutines are faster than running state machines in Update.

more ▼

answered Aug 01 '11 at 12:05 AM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

It's the difference between checking what do to each frame before doing it, and being told when to do something and doing it.

For more in-depth info, read this article. http://altdevblogaday.com/2011/07/07/unity3d-coroutines-in-detail/

Aug 01 '11 at 05:41 AM Joshua

That's a great article. Not only does that explain how coroutines work, but it also elaborates on what exactly the yield keyword does. Anyone working with .Net, also outside Unity, can benefit from that.

Aug 01 '11 at 06:52 AM CHPedersen

@Bovine: I wouldn't have answered the question definitively like that if I didn't have experience in this area. ;) Update isn't magic; it also has overhead (plus it runs every frame whether you need it to or not).

Aug 01 '11 at 04:55 PM Eric5h5

@Bovine, from past experience, simply calling Update() is slower than running a coroutine. Unity calls all your Update()'s through some reflection/indirection that makes the method call alone more expensive.

Aug 03 '11 at 03:36 AM Peter G

@Bovine: No, Apple dropped the "native code" thing last year, that's all.

Aug 12 '11 at 02:02 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left

They are a great things and should definitely be used and exploited, they save performance if used correctly.

Have a read up on; http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html

more ▼

answered Aug 03 '11 at 03:17 AM

vxssmatty gravatar image

vxssmatty
106 4 6 6

I've read the help but it doesn't talk about the cost of starting a coroutine, nesting them and of returning new objects such as WaitForSeconds(). It also doesn't mention the impact on different platforms - specifically mobile!

Aug 03 '11 at 10:30 AM Bovine
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2028
x2014
x678
x336

asked: Jul 31 '11 at 09:39 PM

Seen: 2732 times

Last Updated: Aug 12 '11 at 02:02 AM