- Home /
The question is answered, right answer was accepted
Scripts Optimization
Hello everyone !
I would like to know some things about optimization, I already saw a link in another question which gives some tips, but not I'm asking myself...
I was wondering if it is better to have multiple simple scripts, with short Update() methods, or a few scripts but with large Update() methods. by large I mean containing some mathematical algorithms and stuff like that...
And another question which is quite related, Does Unity handle multithreading by its own correctly or does the programmer needs to do things to have a performant work ?
Thanks in advance !
Graham from Unity has answered this question regarding Update()
http://answers.unity3d.com/questions/552765/few-versus-many-update-routines.html
the answer below is a general discussion on performance program$$anonymous$$g
Answer by TowerOfBricks · May 25, 2012 at 04:15 PM
It might be a tiny bit faster to use one large Update method since you will not have the cost of calling the Update function in the other scripts. But that gain would be tiny in comparison, I would NEVER recommend anyone to write large methods just to gain some performance over having many small methods. Unless you are writing high performance code, and the functions are executed a million times per second or something. Optimization is a very large subject, I suggest that you just google a bit on C# Optimization Tutorial, and you will find stuff like this: C# Optimization
A good tutorial, even though I think some optimizations are overkill for game logic code. Just remember: "Premature Optimization Is The Root Of All Evil" (see wikipedia)
Unity will not handle multithreading on it's own. You will have to learn how to do that yourself. From personal experience I can say that, unless you really really have to, or you think piece of code could be speeded up A LOT, multithreading is not worth it since you introduce all kinds of weird possibilities of bugs in places you thought was straight forward. (simply incrementing a variable in two threads at once can cause bugs). Also, you cannot call any part of the Unity API from other threads than the main thread except Debug.Log and probably some math and utility classes (which do not affect the scene or similar).
what i do is to script freely, not worring about performance, when i have finished a scene with all things.. then a gather all the vars that have the same type (for instance Transform), and put all of them inside an array. do it with each var until you have all encapsulated in arrays... but do this only when you have finished the scene (when there is no more to script).
LawyerOfGod- that is an absolutely terrible approach. It makes your code completely unreadable, uneditable, and does't save you a whole lot.
you must to do it when you have all done, when no need to read, when no need to modify, when no need to do anymore... just when you finished all... it saves a lot of processing time... ( i have done this and it works..) is my experience.
"Saves a lot of processing time" is debatable and probably the result of some other optimization/cleanup.
In my experience, it is very very very rare that every piece of code in a project dies in that project. It's hardly a rule of thumb. Ins$$anonymous$$d, its a good chance that assets you create in one project will then be used in another one.
Follow this Question
Related Questions
How to use get a game object's position in multi-thread? 0 Answers
Using multithreading to speed up an algorithm that interacts with components 1 Answer
How can I get the number of physical cores in the CPU (not logical cores) 1 Answer
Setting Material Textures at runtime for the rest of the game 0 Answers
Should i bake the skinned mesh? 1 Answer