- Home /
The question is answered, right answer was accepted
Performance: many IFs vs one IF with &&'s
How the system checks whether if statements are matched or not? For example #1:
void Update ()
{
bool b0 = false;
bool b1 = true;
bool b2 = true;
if (b0 == true)
{
if (b1 == true)
{
if (b2 == true)
{
}
}
}
}
For example #2:
void Update ()
{
bool b0 = false;
bool b1 = true;
bool b2 = true;
if (b0 == true && b1 == true && b2 == true)
{
}
}
In example #1 the system will check b0, it's not true, so it'll skip everything. So it's just one check per frame. In example #2 the system will check b0, then see it's not true and it'll understand, that there're only &&'s and it'll skip the rest? Or it'll waste its resources on checking the rest? Thus 3 checks per frame, even if b0 is false, making example #1 better for performance? +example #2 also has &&'s which may be require more resources for the system to perceive it as a formula "a && b && d", while example #1 has simple one component one after another.
when do you need this? you maybe should use a for loop or switch...
Same thing as you suspect it will stop the evaluation on the first false it will encounter (of course if you use &&).
Edit: Unless you use quite complex methods to produce the bool values Profiler is useless to check something like this, as proposed below.
even if there was a speed difference between the two statement you wrote. it would be so small and insignificant that it would never effect any preformance with any modern device. and really isn't worth your time to contemplate!!! when it comes to this, use the statement that is visually easy to follow for those who are looking at it.
me personally anything beyond 3 i would do something like this:
bool[] b;
b = new bool[25];
b [9] = true;
int i = b.Length;
while (i>0) {
i--;
if (b [i]) {
print ("bool number "+i+"is true");
break;
}
}
Answer by Bunny83 · Jun 18, 2017 at 12:00 PM
Your two examples are completely equal. Yes "&&" will only evaluate the second operand if the first returns true otherwise it will skip the rest. Likewise "||" only evaluates the second operand when the first is false.
btw: checking a boolean for "true" can be shorten to:
if (b0 && b1 && b2)
to add to this answer, to check if a bool is false you can say:
if(!b0)
Answer by JxWolfe · Jun 18, 2017 at 11:53 AM
I haven't put in the code however there is a window in unity called "Profiler" witch tells you the preprocessing power it takes for the overall game's scripts, rendering, the physics and more... if that script is the only one in the scene with that window and you run it, you will see a script possessing amount then go to the other example and try again...
Follow this Question
Related Questions
Single line if statement with many conditions or several single-conditioned if statements 1 Answer
Checking to see if a loaded level is equal to a member of an integer array 1 Answer
Unity editor low frame rates and flashing while pressing keys 1 Answer
Android framerate increases a little when I put my finger on the screen. What? 0 Answers