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

后端 未结 16 838
灰色年华
灰色年华 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:05

    There is absolutely no difference.

    You might gain points in answering that interview question by referring to the elimination of assignment/comparison typos, though:

    if (flag = 0)  // typo here
       {
       // code never executes
       }
    
    if (0 = flag) // typo and syntactic error -> compiler complains
       {
       // ...
       }
    

    While it's true, that e.g. a C-compiler does warn in case of the former (flag = 0), there are no such warnings in PHP, Perl or Javascript or .

提交回复
热议问题