Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
7
Question by Ceraph · Aug 28, 2013 at 07:44 PM · mobileperformancecoroutineupdate

Is Using Coroutines Actually Faster Than Update?

I've been doing a lot of research lately on how to increase performance of games on mobile and one piece of advice I came across was to use coroutines in place of Unity's built in Update function. It was mentioned on the forums that Update works closer to SendMessage than a standard function call, which causes more of an overhead. I'm very far into my project at this point so I wanted to get some data on the performance difference between Update and coroutines before overhauling my entire project. I created a blank project that has 1000 empty game objects running a simple update and 1000 empty game objects running the operation as a coroutine. The results were overwhelmingly in favor of Update, much to my surprise. Below are the two scripts used to test. Also attached below are the results of the profiler running on an original Motorola Droid. (It is worthwhile to note that this same project was tested on an iPhone 4 with almost identical results).

My best guess is the fact that System.Collections.Generic is not native to Android or iOS so Unity has to include it, which creates an overhead. Does anyone have any ideas why coroutines would be so much slower than Update?

Update:

 private int count;
 private float sine;
 
 // Update is called once per frame
 void Update () 
 {
     count++;
     sine = Mathf.Sin(count / 0.2F);
 }

Coroutine:

 private int count;
 private float sine;
 // Use this for initialization
 void Start () 
 {
     StartCoroutine(AddCount());
 }
 
 private IEnumerator AddCount()
 {
     while (true)
     {
         count++;
         sine = Mathf.Sin(count / 0.2F);
         yield return null;
     }
 }

alt text

alt text

update-overhead.jpg (85.9 kB)
coroutine-overhead.jpg (89.8 kB)
Comment
Add comment · Show 6
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 robhuhn · Aug 29, 2013 at 07:30 AM 0
Share

Thanks for testing. I'm also interested in this.

avatar image Lando1000 · Aug 29, 2013 at 07:51 AM 0
Share

great test, would like to see some more! I was wondering if you could test this with the exact same code in both Update and the coroutine, the reason being, i wonder if the check on the while loop within the coroutine would slow it down and be an unfair test?

avatar image Bunny83 · Aug 29, 2013 at 08:41 AM 1
Share

Coroutines are not "functions". They work completely different. All your code is packed into a statemachine like construct and is hidden in the IEnumerators $$anonymous$$oveNext method. If you enable deep profiling you see calls to subroutines.

@Ceraph: $$anonymous$$ay i ask what android device you use for profiling / debugging? Since the last Android update (Jelly bean 4.3) all Unity development builds crash on startup.

https://code.google.com/p/android/issues/detail?id=58102&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars

avatar image Ceraph · Aug 29, 2013 at 02:53 PM 0
Share

@Bunny83 Thanks for the clarification, I will try and adapt this test to use the event dispatch method. This was tested on an original $$anonymous$$otorola Droid running Android 2.2.3. I also tested it on an iPhone 4 with iOS 6 with similar results but the screencaps above are from the Android test.

@Lando1000 The while loop is there to ensure the coroutine runs every frame, otherwise it would run once on startup and stop.

avatar image Lando1000 · Aug 29, 2013 at 03:09 PM 0
Share

@Ceraph I'm aware of that but because its within a loop i was wondering if the check "while(true)" was causing a slight time increase but from reading the other posts it might be irrelevant

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by Bunny83 · Aug 29, 2013 at 07:46 AM

Of course 1000 coroutines will be more expensive than 1000 Updates. Coroutines are objects which are managed by the coroutine scheduler of Unity. What might be more efficient is to have one dispatcher object with one Update function or a coroutine and dispatch an event to an array / List of objects directly. This of course requires each script to provide a common interface.

Either use, well, and interface or a baseclass with a method that can be overridden in a subclass. The interface approach is more flexible, whereas the basclass approach allows drag and drop in the inspector.

If you "subscribe" your object via script at runtime i would go with an interface.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

22 People are following this question.

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

Related Questions

Is changing 60 FPS into 30 FPS affects time in coroutines? 0 Answers

using coroutines for move objects - performance. 1 Answer

inconsistent results calculating the distance moved by an object 1 Answer

Does a coroutine do all its work during one frame? 1 Answer

Best Performance for Background Movement 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