Interview question: Which one will execute faster, if (flag==0)
or if (0==flag)
? Why?
As others have said, there is no difference.
0
has to be evaluated. flag
has to be evaluated. This process takes the same time, no matter which side they're placed.
The right answer would be: they're both the same speed.
Even the expressions if(flag==0)
and if(0==flag)
have the same amount of characters! If one of them was written as if(flag== 0)
, then the compiler would have one extra space to parse, so you would have a legitimate reason at pointing out compile time.
But since there is no such thing, there is absolutely no reason why one should be faster than other. If there is a reason, then the compiler is doing some very, very strange things to generated code...