Is "else if" faster than "switch() case"?

442,168

Solution 1

For just a few items, the difference is small. If you have many items you should definitely use a switch.

If a switch contains more than five items, it's implemented using a lookup table or a hash list. This means that all items get the same access time, compared to a list of if:s where the last item takes much more time to reach as it has to evaluate every previous condition first.

Solution 2

Why do you care?

99.99% of the time, you shouldn't care.

These sorts of micro-optimizations are unlikely to affect the performance of your code.

Also, if you NEEDED to care, then you should be doing performance profiling on your code. In which case finding out the performance difference between a switch case and an if-else block would be trivial.

Edit: For clarity's sake: implement whichever design is clearer and more maintainable. Generally when faced with a huge switch-case or if-else block the solution is to use polymorphism. Find the behavior that's changing and encapsulate it. I've had to deal with huge, ugly switch case code like this before and generally it's not that difficult to simplify. But oh so satisfying.

Solution 3

Believing this performance evaluation, the switch case is faster.

This is the conclusion:

The results show that the switch statement is faster to execute than the if-else-if ladder. This is due to the compiler's ability to optimise the switch statement. In the case of the if-else-if ladder, the code must process each if statement in the order determined by the programmer. However, because each case within a switch statement does not rely on earlier cases, the compiler is able to re-order the testing in such a way as to provide the fastest execution.

Solution 4

Another thing to consider: is this really the bottleneck of your application? There are extremely rare cases when optimization of this sort is really required. Most of the time you can get way better speedups by rethinking your algorithms and data structures.

Solution 5

Switch is generally faster than a long list of ifs because the compiler can generate a jump table. The longer the list, the better a switch statement is over a series of if statements.

Share:
442,168
P. Duw
Author by

P. Duw

Updated on September 22, 2021

Comments

  • P. Duw
    P. Duw over 2 years

    I'm an ex Pascal guy, currently learning C#. My question is the following:

    Is the code below faster than making a switch?

    int a = 5;
    
    if (a == 1)
    {
        ....
    }
    else if(a == 2)
    {
        ....
    }
    else if(a == 3)
    {
        ....
    }
    else if(a == 4)
    {
        ....
    }
    else
        ....
    

    And the switch:

    int a = 5;
    
    switch(a)
    {
        case 1:
            ...
            break;
    
        case 2:
            ...
            break;
    
        case 3:
            ...
            break;
    
        case 4:
            ...
            break;
    
        default:
            ...
            break;
    
    
    }
    

    Which one is faster?

    I'm asking, because my program has a similar structure (many, many "else if" statements). Should I turn them into switches?