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

后端 未结 16 734
灰色年华
灰色年华 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 <insert language here>.

    0 讨论(0)
  • 2020-11-30 18:05

    Build two simple programs using the suggested ways.

    Assemble the codes. Look at the assembly and you can judge, but I doubt there is a difference!

    Interviews are getting lower than ever.

    0 讨论(0)
  • 2020-11-30 18:08

    I haven't seen any correct answer yet (and there are already some) caveat: Nawaz did point out the user-defined trap. And I regret my hastily cast upvote on "stupidest question" because it seems that many did not get it right and it gives room for a nice discussion on compiler optimization :)

    The answer is:

    What is flag's type?

    In the case where flag actually is a user-defined type. Then it depends on which overload of operator== is selected. Of course it can seem stupid that they would not be symmetric, but it's certainly allowed, and I have seen other abuses already.

    If flag is a built-in, then both should take the same speed.

    From the Wikipedia article on x86, I'd bet for a Jxx instruction for the if statement: perhaps a JNZ (Jump if Not Zero) or some equivalent.

    I'd doubt the compiler misses such an obvious optimization, even with optimizations turned off. This is the type of things for which Peephole Optimization is designed for.

    EDIT: Sprang up again, so let's add some assembly (LLVM 2.7 IR)

    int regular(int c) {
      if (c == 0) { return 0; }
      return 1;
    }
    
    int yoda(int c) {
      if (0 == c) { return 0; }
      return 1;
    }
    
    define i32 @regular(i32 %c) nounwind readnone {
    entry:
      %not. = icmp ne i32 %c, 0                       ; <i1> [#uses=1]
      %.0 = zext i1 %not. to i32                      ; <i32> [#uses=1]
      ret i32 %.0
    }
    
    define i32 @yoda(i32 %c) nounwind readnone {
    entry:
      %not. = icmp ne i32 %c, 0                       ; <i1> [#uses=1]
      %.0 = zext i1 %not. to i32                      ; <i32> [#uses=1]
      ret i32 %.0
    }
    

    Even if one does not know how to read the IR, I think it is self explanatory.

    0 讨论(0)
  • 2020-11-30 18:08

    Just as an aside ( I actually think any decent compiler will make this question moot, since it will optimise it ) using 0 == flag over flag == 0 does prevent the typo where you forget one of the = ( ie if you accidently type flag = 0 it will compile, but 0 = flag will not ), which I think is a mistake everyone has made at one point or another...

    0 讨论(0)
提交回复
热议问题