- Home /
is the process of checking if a boolean is true then setting it to false faster than just setting it to false without checking its state ?
as the question states , i would like to know if it is faster to just set a boolean to false whether or not it is true of false , or is checking if the bool is true then setting it to false; faster ?
and over time with an accumulation if objects referencing the bool with it still be the fastest method ?
What happens if it false? Nothing? By the way, this is micro optimization.
yeah, nothing happens if it is false.
if(abool) abool = false;
abool = false;
abool = (abool) ? !abool:abool;
To expand on the micro-optimization comment, tommorow you'll realize you do need to check if it was true, before setting to false, for some error checks, clean up, etc... .
Answer by mattyman174 · Mar 13, 2015 at 04:57 AM
if(myBool) // I need to check if the boolean is true before assigning a new value to it, first step.
myBool = false; // Now assigning it a value, second step.
myBool = true; // Just assigning its value without a check only takes 1 step.
Checking the variable first introduces another step of complexity to it. Just assigning its value without checking is faster.
I doubt you would see any marked improvement in performance if you stopped checking a simple data type such as a boolean, infact it would probably hurt your code if you stopped checking what data is being held in your variables.
I think he means
if(myBool)
myBool = false;
myBool = !myBool;
Good point, didnt think of that. Though i would still think that it would be faster without checking its state using an IF statement.
Toggling is still just simply an assignment operation and with a simple data type like a boolean it would just be flipped.
I agree on that point but with compiler optimization, it is also possible that both code will yield identical asm codes.
$$anonymous$$y best guess is that if(myBool) myBool = false
equals myBool = true
on C# compilers. But for other codes like if(myBool) {...}
and myBool = !myBool; ...
, I'm betting that the latter is way better.
Your probably right. I prefer toggle ins$$anonymous$$d of checking its value first as i think it reads better.
myBool ^= true;
I would even say an XOR would result the same as well as far as i understand it.
There's nothing in the OP to suggest it's about toggling (it only ever mentions setting the bool to false
). So the code in the answer should be...
if(myBool) // I need to check if the boolean is true before assigning a new value to it, first step.
myBool = false; // Now assigning it a value, second step.
myBool = false; // Just assigning its value without a check only takes 1 step.
But regardless, the analysis seems right to me. The first cannot be faster than the second, but the difference is probably not all that useful.
Your answer
![](https://koobas.hobune.stream/wayback/20220613185955im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a bubble level (not a game but work tool) 1 Answer
bool doesn't change?? 0 Answers
An OS design issue: File types associated with their appropriate programs 1 Answer