0

Is using a switch() statement more or less efficient then several if() statements in C? Does switch() compile to multiple if() statements? For example:

int a = 5;
switch(a) {
    case 5:
        printf("Number 5\n");
        break;
    case 3:
        printf("Number 3\n");
        break;
    default:
        printf("Other\n");
        break;
}

versus:

int a = 5;
if (a == 5) {
    printf("Number 5\n");
}
if (a == 3) {
    printf("Number 3\n");
}
if (a != 5 && a != 3) {
    printf("Other\n");
}
wispi
  • 431
  • 3
  • 14
  • 1
    Compilers have at least three different ways to implement `switch` statements and will pick the best one based on the range and density of the numbers used in the `case` statements. So worst case, it's the same as multiple `if` statements, but usually performs better. – user3386109 Oct 24 '18 at 22:50
  • Usually either into the equivalent of nested ifs or into a jump table whatever looks better to the compiler. It's pretty much certain to be quite a bit faster than a trivial function call (much faster compared to a relatively expensive fn call such as a call to printf) If you're worried about it benchmark it. A bit of well-crafted benchmarking code will give you much better performance-related answers than human speculators on the internet will. – Petr Skocik Oct 24 '18 at 22:52
  • 2
    An effective optimizing compiler would be able to treat these pieces of code just the same, IIANM. (Of course assuming you haven't overloaded `operator==` or other such trickery). – einpoklum Oct 24 '18 at 22:52
  • 1
    @einpoklum That's a safe assumption to make because C doesn't have overloading :) – P.P Oct 24 '18 at 22:58
  • 1
    @P.P.: Sorry, I'm too used to C++ nitpickers telling you "Oh, but what if I've overloaded `operator,` ?" Or "But what if my `operator==` has side-effects?`" - I just instinctively deflect... – einpoklum Oct 24 '18 at 23:09
  • 1
    Trust me, if you were working on a project where the bottleneck was a "pick a branch", you'd either already know which one is faster or your project is complete mess and should be destroyed physically with the whole hard drive itself. As for @einpoklum, maybe you should stop talking to skids, because the difference is negligible whether you overloaded the operator or not? If your project is so perfect that the only way you can optimize it is to remove operator overload and rewrite if ladder to switch statement, then congratulations, your company has a perfect programmer team. – Purple Ice Oct 25 '18 at 09:51
  • @PurpleIce: That's also quite true. It sounds like this is premature optimization - which, as [Donald Knuth tells us, is the root of all evil](https://en.wikiquote.org/wiki/Donald_Knuth). – einpoklum Oct 25 '18 at 11:55
  • This is worse than premature optimization, it's opmization where's nothing to optimize. This reminds me of things like "is it better to use `i++` or `++i`", "should I bitshift to multiply or just multiply?", "should I divide by `2` or should I multiply by `0.5`" and the likes... By the way, correct answer is "whichever one is more readable, use up to date compiler for your platform and use `-O1` or greater". – Purple Ice Oct 25 '18 at 13:50

0 Answers0