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
2
Question by Tray · Jun 08, 2010 at 04:09 AM · coroutineyield

How often should I Yield the next frame in a coroutine for performance optimization?

So I have a Coroutine that performs some expensive operations like serialization, md5 hash and some compression algorithms.

Is is better for me to place my own "yield return null;" statement after every line in my script so I don't block the main thread or is the coroutine scheduler in Unity only running a few instructions each frame anyway and then yielding.

The caller of my coroutine is basically waiting for it to finish before proceeding.

I can place these yield statements in but my code becomes messy and very ugly.

IEnumerator KickItOff() { yield return StartCoroutine(ExpensiveOps()); }

IEnumerator ExpensiveOps() { serialize(); yield return null; // do I need these?

 hash();
 yield return null;

 compress();
 yield return null;

 WWW post = new WWW("...",data);
 yield return post;

}

How far is this suppose to go then? Every couple of instructions needs a yield so as not to block? Or just play it by ear?

I could always spawn my own threads to do some of these calculations and take care not to call the Unity API on that thread. Ideally I wouldn't have to do either.

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

2 Replies

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

Answer by Eric5h5 · Jun 08, 2010 at 04:23 AM

Unity doesn't interrupt coroutines until you use a yield; coroutines wouldn't work at all otherwise. I don't think those yield return nulls will really accomplish anything except to make the routine take a few extra frames longer to run...if you actually want to make coroutines work "sort of like threads", then you'd have to make the expensive routines themselves into coroutines and try yielding every few loops or something. That's pretty crude though. The only real way to make functions work like separate non-blocking threads is to use actual threads.

Comment
Add comment · 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 qJake · Jun 08, 2010 at 08:18 AM 0
Share

Threads are not coroutines! Eric is correct, if you need to use a thread, use it, don't substitute Coroutines in their place. :)

avatar image
0

Answer by PaulUsul · Jun 08, 2010 at 10:44 AM

I have had the same problem, when doing heavy duty calculations. And coroutines do block and they do not call gc, so memory that is no longer referenced is not cleaned. So I usually call Wait For End Of Framme at the end of a "for" for-example. But I have only ever needed it where calculations took minutes. It really depends on whether you want frames or speedy results.

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

No one has followed this question yet.

Related Questions

How to set timer for WWW helper? 1 Answer

Execution manner of co routine 2 Answers

Yield Wait for Seconds (A little help here) 2 Answers

Substracting Lives every n seconds on TriggerEnter 2 Answers

Create a 10 second delay using WaitForSeconds 2 Answers


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