Faster assignment or check for bool values
boolean, c#, variable-assignment
Solution
I prefer to measure for these sorts of questions rather than guess:
- CalledOften1: 52 million operations per second
- CalledOften2: 53 million operations per second
So they are nearly the same. If anything, the simpler method is also the faster.
Problem
The question is simple, which is faster between CalledOften1 and CalledOften2 ``` class MyTest { public bool test = false; void CalledOften1() { if (!test) test = true; DoSomething(); } void CalledOften2() { test = true; DoSomething(); } } ``` Is the compiler optimized (if possible) to avoid future assignments of test if it's already true? UPDATE: This question is just an information, I will not use the if (bla) style if I can write test=true, I prefer code readability.