Advantage of switch over if-else statement

后端 未结 22 2146
梦谈多话
梦谈多话 2020-11-22 11:08

What\'s the best practice for using a switch statement vs using an if statement for 30 unsigned enumerations where about 10 have an ex

相关标签:
22条回答
  • 2020-11-22 11:57

    IMO this is a perfect example of what switch fall-through was made for.

    0 讨论(0)
  • 2020-11-22 12:00

    For the special case that you've provided in your example, the clearest code is probably:

    if (RequiresSpecialEvent(numError))
        fire_special_event();
    

    Obviously this just moves the problem to a different area of the code, but now you have the opportunity to reuse this test. You also have more options for how to solve it. You could use std::set, for example:

    bool RequiresSpecialEvent(int numError)
    {
        return specialSet.find(numError) != specialSet.end();
    }
    

    I'm not suggesting that this is the best implementation of RequiresSpecialEvent, just that it's an option. You can still use a switch or if-else chain, or a lookup table, or some bit-manipulation on the value, whatever. The more obscure your decision process becomes, the more value you'll derive from having it in an isolated function.

    0 讨论(0)
  • 2020-11-22 12:00

    The switch is faster.

    Just try if/else-ing 30 different values inside a loop, and compare it to the same code using switch to see how much faster the switch is.

    Now, the switch has one real problem : The switch must know at compile time the values inside each case. This means that the following code:

    // WON'T COMPILE
    extern const int MY_VALUE ;
    
    void doSomething(const int p_iValue)
    {
        switch(p_iValue)
        {
           case MY_VALUE : /* do something */ ; break ;
           default : /* do something else */ ; break ;
        }
    }
    

    won't compile.

    Most people will then use defines (Aargh!), and others will declare and define constant variables in the same compilation unit. For example:

    // WILL COMPILE
    const int MY_VALUE = 25 ;
    
    void doSomething(const int p_iValue)
    {
        switch(p_iValue)
        {
           case MY_VALUE : /* do something */ ; break ;
           default : /* do something else */ ; break ;
        }
    }
    

    So, in the end, the developper must choose between "speed + clarity" vs. "code coupling".

    (Not that a switch can't be written to be confusing as hell... Most the switch I currently see are of this "confusing" category"... But this is another story...)

    Edit 2008-09-21:

    bk1e added the following comment: "Defining constants as enums in a header file is another way to handle this".

    Of course it is.

    The point of an extern type was to decouple the value from the source. Defining this value as a macro, as a simple const int declaration, or even as an enum has the side-effect of inlining the value. Thus, should the define, the enum value, or the const int value change, a recompilation would be needed. The extern declaration means the there is no need to recompile in case of value change, but in the other hand, makes it impossible to use switch. The conclusion being Using switch will increase coupling between the switch code and the variables used as cases. When it is Ok, then use switch. When it isn't, then, no surprise.

    .

    Edit 2013-01-15:

    Vlad Lazarenko commented on my answer, giving a link to his in-depth study of the assembly code generated by a switch. Very enlightning: http://lazarenko.me/switch/

    0 讨论(0)
  • 2020-11-22 12:00

    They work equally well. Performance is about the same given a modern compiler.

    I prefer if statements over case statements because they are more readable, and more flexible -- you can add other conditions not based on numeric equality, like " || max < min ". But for the simple case you posted here, it doesn't really matter, just do what's most readable to you.

    0 讨论(0)
  • 2020-11-22 12:00

    Aesthetically I tend to favor this approach.

    unsigned int special_events[] = {
        ERROR_01,
        ERROR_07,
        ERROR_0A,
        ERROR_10,
        ERROR_15,
        ERROR_16,
        ERROR_20
     };
     int special_events_length = sizeof (special_events) / sizeof (unsigned int);
    
     void process_event(unsigned int numError) {
         for (int i = 0; i < special_events_length; i++) {
             if (numError == special_events[i]) {
                 fire_special_event();
                 break;
              }
         }
      }
    

    Make the data a little smarter so we can make the logic a little dumber.

    I realize it looks weird. Here's the inspiration (from how I'd do it in Python):

    special_events = [
        ERROR_01,
        ERROR_07,
        ERROR_0A,
        ERROR_10,
        ERROR_15,
        ERROR_16,
        ERROR_20,
        ]
    def process_event(numError):
        if numError in special_events:
             fire_special_event()
    
    0 讨论(0)
  • 2020-11-22 12:00

    I know its old but

    public class SwitchTest {
    static final int max = 100000;
    
    public static void main(String[] args) {
    
    int counter1 = 0;
    long start1 = 0l;
    long total1 = 0l;
    
    int counter2 = 0;
    long start2 = 0l;
    long total2 = 0l;
    boolean loop = true;
    
    start1 = System.currentTimeMillis();
    while (true) {
      if (counter1 == max) {
        break;
      } else {
        counter1++;
      }
    }
    total1 = System.currentTimeMillis() - start1;
    
    start2 = System.currentTimeMillis();
    while (loop) {
      switch (counter2) {
        case max:
          loop = false;
          break;
        default:
          counter2++;
      }
    }
    total2 = System.currentTimeMillis() - start2;
    
    System.out.println("While if/else: " + total1 + "ms");
    System.out.println("Switch: " + total2 + "ms");
    System.out.println("Max Loops: " + max);
    
    System.exit(0);
    }
    }
    

    Varying the loop count changes a lot:

    While if/else: 5ms Switch: 1ms Max Loops: 100000

    While if/else: 5ms Switch: 3ms Max Loops: 1000000

    While if/else: 5ms Switch: 14ms Max Loops: 10000000

    While if/else: 5ms Switch: 149ms Max Loops: 100000000

    (add more statements if you want)

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