Switch vs if else

Started by
19 comments, last by L. Spiro 11 years, 5 months ago
Hello, Is there any difference between using a switch statement and maybe 5 if else statements? By difference i mean performance wise,
Advertisement
Your compiler is highly likely to generate the exact same code for both, but it is dependent on your compiler.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

so it is just preference wise?
Use whichever gives you the cleanest code.

This is honestly a micro-optimization in 99% of cases, and any performance-related preference for one over the other should be backed up by profiling. See http://stackoverflow.com/questions/2086529/what-is-the-relative-performance-difference-of-if-else-versus-switch-statement-i for further discussion, Java-specific but much of it is generally applicable.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

I will use a switch statement if I'm dealing with a lot of checks. If it's just a few, "else if" will be good enough. They will all accomplish the same thing, code wise.
GameDev Journal: http://www.gamedev.n...-rooks-journal/

OpenChess - 1.0 done!

Classic RPG #1 - Task 9 -> January 1st 2013
All other things being equal, switch expresses your intent more clearly. While it may not matter to the compiler, it will matter to readers of your code including your future self. Having a bunch of else ifs will force them to examine the whole construct in detail to hunt down the reason, why it is not a simple switch/case.

So, in short, prefer switch/case over else/ifs.
While this might be language-dependent, you can't normally switch-case objects like strings. switch-case could only be used on integers, as it creates a jump/branch table. Additonally, in some languages, you are allowed to fall through a case statement.

switch (x) {
case 0:
doSomething();
case 1:
doSomethingElse();
break;
default:
doDefault();
break;
}


if x = 0, doSomething() and doSomethingElse() are executed. This style of coding could be useful in certain cases (no pun intended).

While this might be language-dependent, you can't normally switch-case objects like strings. switch-case could only be used on integers, as it creates a jump/branch table. Additonally, in some languages, you are allowed to fall through a case statement.

switch (x) {
case 0:
doSomething();
case 1:
doSomethingElse();
break;
default:
doDefault();
break;
}


if x = 0, doSomething() and doSomethingElse() are executed. This style of coding could be useful in certain cases (no pun intended).


You are right. This is a language dependent thing because i know that in java 1.7 switch statements support strings
/********************************************************************************\
/**********************He Who Dares, Wins**********************************\
/********************************************************************************\
C# switches support quite a few datatypes. String and char being 2 examples. Any datatype usually represented as a class though does not work such as XNA's gamepad state object. C# also does not allow fall throughs so although the basic principle is the same there are language dependant alterations.
This thread is about using switch in C++. You can only use switch on an integer type, and it is particularly common to use it on an enum.

If you need to use a switch with a string and performance is critical, convert the string to an enum and then switch on the enum. This conversion can be done easily with a hash (unordered_map, in C++11 speak) or you can implement a trie, which is even faster in some cases. I've only ever needed to do this once in 30 years of programming experience.

This topic is closed to new replies.

Advertisement