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

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

    There will be no difference in your versions.

    I'm assuming that the type of flag is not user-defined type, rather it's some built-in type. Enum is exception!. You can treat enum as if it's built-in. In fact, it' values are one of built-in types!

    In case, if it's user-defined type (except enum), then the answer entirely depends on how you've overloaded the operator == . Note that you've to overload == by defining two functions, one for each of your versions!

    0 讨论(0)
  • 2020-11-30 17:45

    There will be absolutely no difference speed-wise. Why should there be?

    0 讨论(0)
  • 2020-11-30 17:47

    I think the best answer is "what language is this example in"?

    The question did not specify the language and it's tagged both 'C' and 'C++'. A precise answer needs more information.

    It's a lousy programming question, but it could be a good in the devious "let's give the interviewee enough rope to either hang himself or build a tree swing" department. The problem with those kinds of questions is they usually get written down and handed down from interviewer to interviewer until it gets to people who don't really understand it from all the angles.

    0 讨论(0)
  • 2020-11-30 17:48

    They should be exactly the same in terms of speed.

    Notice however that some people use to put the constant on the left in equality comparisons (the so-called "Yoda conditionals") to avoid all the errors that may arise if you write = (assignment operator) instead of == (equality comparison operator); since assigning to a literal triggers a compilation error, this kind of mistake is avoided.

    if(flag=0) // <--- typo: = instead of ==; flag is now set to 0
    {
        // this is never executed
    }
    
    if(0=flag) // <--- compiler error, cannot assign value to literal
    {
    
    }
    

    On the other hand, most people find "Yoda conditionals" weird-looking and annoying, especially since the class of errors they prevent can be spotted also by using adequate compiler warnings.

    if(flag=0) // <--- warning: assignment in conditional expression
    {
    
    }
    
    0 讨论(0)
  • 2020-11-30 17:51

    When in doubt benchmark it and learn the truth.

    0 讨论(0)
  • 2020-11-30 17:51

    Which one's fast depends on which version of == you are using. Here's a snippet that uses 2 possible implementations of ==, and depending on whether you choose to call x == 0 or 0 == x one of the 2 is selected.

    If you are just using a POD this really shouldn't matter when it comes to speed.

    #include <iostream>
    using namespace std;
    
    class x { 
      public:
      bool operator==(int x) { cout << "hello\n"; return 0; }
      friend bool operator==(int x, const x& a) { cout << "world\n"; return 0; } 
    };
    
    int main()
    { 
       x x1;
       //int m = 0;
       int k = (x1 == 0);
       int j = (0 == x1);
    }
    
    0 讨论(0)
提交回复
热议问题