Update vs. FixedUpdate vs. Coroutine = while(true) yield return null;

We can use:

Update()

FixedUpdate()

{
while(true)
{
yield return null; // SameFPS as Update?
// yield return waitForFrameUpdate; instead?
// Not sure which one, but the point im making stands.
}
}```

WaitForSeconds customDeltaTime = new WaitForSeconds(0.02f); // Can change this to 20FPS for better perf?
IEnumerator CustomUpdate()
{
while(true)
{
yield return customDeltaTime; // Same FPS as Fixed?
}
}```

I want to find the most performant loop, will yield return null allocate anything?
Or is WaitForSeconds expensive?

Should we just use Update? (I think its really smelly to update something so much its nonsensical with high framerates)
As suggested by someone else on the forum, use a timer inside Update… but that adds another if statement and more overhead for calculating/keeping track of the float timer…
It seems to me Coroutines are best for updating values/objects/whatever at low intervals(like 20fps).
Unless Coroutines are actually doing some silly stuff/ create allot of garbage/allocations/overhead under the hood.

Thanks men

Use the profiler.

2 Likes

Im more interested in the under the hood workings of these things and learnin why something is better than the others…

What’s there to be under the hood? Update is called every frame. FixedUpdate is called to catch up with its accumulator. Coroutines tick away at whatever interval you set.

You need to stop bogging yourself down with theoreticals and measure the results yourself with the damn profiler.

4 Likes

Agreed, use the profiler. If you really want to know, spend 30 minutes creating a simple stress test and use the profiler to give you the answer on the device(s) you’re using.

Asking, waiting, discussing and believing what others say isn’t always the best approach for this. The golden ruled “best” doesn’t often exist. :slight_smile:

Most of the time, the code you’re running is the predominant factor here. Running less code less often is the key. For details, the profiler should tell you.

5 Likes

EDIT : deleted bad joke

In that case, I wouldn’t worry about performance. :wink:

1 Like

Thanks Melv, I will check out this profiler thingy, seems pretty interesting.

No just kidding again, I know the profiler, but I thought it was pretty inaccurate, I will run some tests and figure it out anyway, I think its best to create a new project for these test cases so its completely fresh?

1 Like

Yeah that’s not the case but even if it were, it’s more accurate than what devs “think”. :slight_smile:

3 Likes

If in fact one is using the profiler on a stand alone build. Using it in the Editor can give different results*

Of course, there’s an overhead in the Editor and good to point out, but to be clear, this isn’t really the subject here. It’s about relative peformance between different things, not how fast something very specific is running; you’ll clearly only get that on the specific device.

Profilers such as the built-in profiler or something such as Superluminal are the only answer.

Here are some links that demystify Update(), FixedUpdate(), and coroutines.

https://blog.unity.com/engine-platform/10000-update-calls
https://discussions.unity.com/t/530594
https://stackoverflow.com/questions/12932306/how-does-startcoroutine-yield-return-pattern-really-work-in-unity

1 Like

Here’s two more good ones to check out:

2 Likes