- Home /
Few versus many Update() routines?
I've often wondered .. is there any advantage to minimising your Update() routines?
Say you have 100 scripts and 20 use Update(). Compare to "centralising" so that you have only one or two scripts with Update().
Performance-wise - with the specific issue of Update() calls in the Unity milieu - is there any difference?
Is this something everyone knows about other than me as usual? Any thoughts? Cheers!
I asked a question close to this one some time ago.
here it is.
Hi Fattie,
I believe that the fewer methods that get called the better the performance, because every method that gets called carries some overhead. This is much more so, with the Update() method, because it gets called every frame (as I'm sure everybody in the Unity community knows). So, the overhead inherent on method calling occurs in every frame.
So, if it were possible to include the 1,000 lines of code originally located in each of 10 Update() methods, to 1 Update() method containing 10,000 lines of code, there would be an overall performance gain.
However, I can see 2 issues with this:
a. In order to "centralize" the code in a single Update, it would most likely be necessary to create references to decentralized code. This would probably create a performance loss that would be greater than the performance gain from centralizing.
b. If there is plenty of code inside the original Update() methods, then the majority of the workload would be in executing the code, and the performance hit for calling this Update() method would be insignificant.
Bottom of the Line: I would be concerned with centralizing Update() methods, only if I had a large number of them, with each containing only very few lines of "light" code.
Hi @pako, you're just re-stating the question.
"because every method that gets called carries some overhead", yes, obviously, the question is: what's the overhead involved with Update callbacks in the overall unity - mono - monobehaviour - component milieu. (just branching to a routine is nothing)
"the performance hit for calling this Update() method..." is unknown, it's what I'm asking
If you google something like "c# method call performance" you'll get some interesting links. Basically, it all comes down to testing it yourself if you want actual facts. The Stopwatch Class is useful for this:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx
hey pako, naturally I could do the work myself - but that's no fun. better to ask here :) it's difficult to believe this isn't a well-known topic that has been well-explored
Answer by Graham-Dunnett · Oct 10, 2013 at 10:49 AM
The cost of calling multiple Update()
functions is completely lost in the noise. Unity has a list of objects with Update()
functions and calls them in turn. (Or the order you specify.)
"completely lost in the noise" $$anonymous$$agnificent - thanks a million for that, GD!!!
"is completely lost in the noise" - what does that mean?
Your answer
Follow this Question
Related Questions
solution for long simple animation? 1 Answer
Performance when having Update functions in many gameObjects ? 1 Answer
MonoBehaviour Update() performance vs handling updates in a single UpdateManager script 0 Answers
Measuring all the Update loops combined 1 Answer
Animator.Play() in update, Performance?? 0 Answers