I didnt manage to find any similar question so its good to ask. As the title suggests what is better for me to do in a function update if i just need the change a variable once ?
example code:
var test1 : int;
function Update() {
if (otherScriptName.otherStaticVariable == 500) { test1 = otherScriptName.otherStaticVariable; }
if (otherScriptName.otherStaticVariable == 100) { test1 = otherScriptName.otherStaticVariable; }
}
OR
var test1 : int;
var done : boolean;
function Update() {
if (otherScriptName.otherStaticVariable == 500 && !done) {
test1 = otherScriptName.otherStaticVariable;
done = true;
}
if (otherScriptName.otherStaticVariable == 100 && !done) {
test1 = otherScriptName.otherStaticVariable;
done = true;
}
}
ALSO (as i was writing i thought of that too)
var test1 : int;
function Update() {
if (otherScriptName.otherStaticVariable == 500 || otherScriptName.otherStaticVariable == 100) {
test1 = otherScriptName.otherStaticVariable;
}
}
My main question is if there is any performance difference between the 2 first examples lets say if i have each of them in 900 differenct objects.
(Ofcourse this is an example code my real code is much bigger but its almost the same method and i want to know if it has an impact on performance even slightly)
Measure performance and determine where most CPU time, memory or other resource usage is occuring
Change code in slowest/largest/most expensive areas
Measure performance in the same way as in step 1 and compare
If performance did not measurably improve then undo changes from step 2
If not fast/small/whatever enough go back to step 1
Also, high level optimisations first. If this sort of thing is genuinely causing you a performance problem then JavaScript is the wrong language for you. A good first step would be to use #pragma strict in the hopes it does something for the compiler. A next step would be to use C# and another would be to not even use Unity…
var test1 : int;
function Update()
{
if ((!done) && (otherScriptName.otherStaticVariable == 500 || otherScriptName.otherStaticVariable == 100))
{
test1 = otherScriptName.otherStaticVariable;
}
}
This would be the cleanest and most efficient. First the code will check the local var if it is not done, and if it’s done, then it skips everything else. But if it’s !done, then it goes on to check the second argument, which is an either or, but only when !done.
When the first argument in an && statement comes back false, it doesn’t bother with the rest, as there’s no point, since both need to be true for the statement to pass.
When using too many variables to maintain state is usually a bad idea and will put you on the late night - early morning debugging fast track. I would suggest writing state machine type logic using an enum if it’s applicable to your scenario.
...
public enum State { State1 = 100, State2 = 500, Done=0 };
...
void Update()
{
switch(state)
{
case State.State1:
...
state = State.State2;
break;
case State.State2:
state = State.Done;
break;
default:
...
break;
}
}
But I have to point out that assigning a value to a variable is a lightning fast operation. Trying to avoid “unnecessary” assignments with checks like these may actually be slower than just assigning every time (check this question).
And, in theory, removing the check also eliminates potential branching errors. So I’d say it’s good practice to try to avoid a check like this if it’s not necessary for the logic and it doesn’t improve readability.