- Home /
Scripts optimisation
So, if I need to set a variable in a update or in a ongui function, is it better to type for example
x=y
or it's better
if (x != y)
x=y;
?
Answer by DESTRUKTORR · Aug 11, 2012 at 12:44 PM
The former would likely be more efficient. It's really difficult to say, since this is one of those cases where checking and assigning take roughly the same amount of memory, but it's a much better habit to simply avoid putting things like this in your update methods, as the numbers add up, over time. Try to find other ways of detection (interaction through triggers, collisions, or direct-activation via another class works pretty nicely).
Overall, though, for a one line assigning, the former of your two options is more efficient.
so it's better to check if it's different (if) and then assign the value only if different than to assign the value each update/ongui function?
Answer by TheDavil86 · Aug 11, 2012 at 03:22 PM
using #pragma strict will generally help code be a lot more efficient as it forces you to not use much in terms of references or searching for variables with things like the .Find function.
For your specific question, evaluating is going to use more processing power and memory than just doing the assignment. The best answer is to think of when the variable is going to be changed and only actually update it when necessary. The update function isn't really needed all that often unless something is visible to the player in my experience.
well, I'm using c# so I don't have the #pragma strict problem, the point is that in my editor dialog I have to check if a texture changes so I'll put the EditorGUILayout.Objectfield with the texture and check(in the ongui function) if the texture is different from the previous one and assign it back to the terrain
Well in that case for textures you'll want to do the evaluation due to rendering taking longer than just checking whether or not it actually needs to be done.
Your answer
Follow this Question
Related Questions
How Can I Reduce Build Time for Script-Heavy Projects? 2 Answers
Dll call 10x slower in Unity 3 Answers
Which Unity functions are generally slow/bad for performance? 4 Answers
Is Instantiating bullets/many objects always bad for performance? 1 Answer
Are there any performance differences between a timer and using Invoke? 1 Answer