• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Bovine · Jul 31, 2011 at 09:39 PM · iosiphoneperformancecoroutine

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

Comment
FTheCloud
Rafes
kayy
ElectricMonk
Ilgiz

People who like this

5 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Eric5h5 · Aug 01, 2011 at 12:05 AM

Coroutines are faster than running state machines in Update.

Comment
Joshua
Bovine
Peter G
Rush-Rage-Games
Macarse
miltonsegura
ElectricMonk
Ilgiz

People who like this

8 Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joshua · Aug 01, 2011 at 05:41 AM 2
Share

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/

avatar image CHPedersen · Aug 01, 2011 at 06:52 AM 0
Share

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.

avatar image Eric5h5 · Aug 01, 2011 at 04:55 PM 1
Share

@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).

avatar image Peter G · Aug 03, 2011 at 03:36 AM 1
Share

@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.

avatar image Eric5h5 · Aug 12, 2011 at 02:02 AM 1
Share

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

Show more comments
avatar image

Answer by vxssmatty · Aug 03, 2011 at 03:17 AM

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

Comment
Joshua

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bovine · Aug 03, 2011 at 10:30 AM 0
Share

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!

avatar image

Answer by kayy · Sep 05, 2013 at 11:21 AM

2 years later but maybe interesting for others:

Using Unity 3.5.7 on my old iPhone 4 I did some profiling. As others stated, coroutines are almost for free, but the profiler revealed that instantiating a YieldInstruction could take a measurable amount of time.

So I replaced all:

 while (myBool) {
     // do something
     yield return new WaitForEndOfFrame(); // 0.2 - 2 ms every frame
 }

by

 YieldInstruction yieldInstruction = new WaitForEndOfFrame ();
 while (myBool) {
     // do something
     yield return yieldInstruction; // no impact
 }
Comment
terrypaton1
EDevJogos

People who like this

2 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JoshOClock · Jun 17, 2014 at 08:42 PM 0
Share

This is interesting info. I've had problems with Coroutines having unreasonable amounts of performance overhead. They might be faster than Update() but I'm not sure they would be faster than Invoke("") or changing state based on events.

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why my App size increases four times after playing on Ios 0 Answers

ex2D performance 1 Answer

Random "This app may slow down you phone" warnings even on 64 bit 0 Answers

Procedural mesh update slow on iphone 4 1 Answer

Unity Screen.SetResolution gives me black screen on iPhone 4S 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges