Which one will execute faster, if (flag==0) or if (0==flag)?

后端 未结 16 786
灰色年华
灰色年华 2020-11-30 17:12

Interview question: Which one will execute faster, if (flag==0) or if (0==flag)? Why?

16条回答
  •  無奈伤痛
    2020-11-30 18:00

    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...

提交回复
热议问题