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

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

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

16条回答
  •  时光说笑
    2020-11-30 17:57

    Well, I am agreeing completely with all said in the comments to the OP, for the exercise sake:

    If the compiler is not clever enough (indeed you should not use it) or the optimization is disabled, x == 0 could compile to a native assembly jump if zero instruction, while 0 == x could be a more generic (and costly) comparison of numeric values.

    Still, I wouldn't like to work for a boss who thinks in these terms...

提交回复
热议问题