Switch vs. If/Else

Started by
17 comments, last by the_grip 22 years, 2 months ago
i''ve heard it said that switch statements run slightly faster than if/else if/else statements... can anyone verify this?
"You call him Dr. Grip, doll!"
Advertisement
Well, I guess a switch could be a bit faster to use instead of a lot of if, since you usually break out of the switch statement when a case is true so the computer wont have to check the other cases. But I''m just guessing here. And there may be some other differences too...


-----------------------------
"problems have solutions
a lifetime of fucking things up fixed in one determined flash"
- The Downward Spiral, NIN
-----------------------------Reporter: Are they slow-moving, chief?Sheriff: Yeah, they're dead. They're all messed up.-Night of the living dead
i don't know about the speed, but i just use whatever i think is neater.
and logically
    if (){}else {}should be faster thanif (){}else if (){}    

although i haven't done any testing on it.


Edited by - thuned on January 30, 2002 11:24:38 AM
life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)
AFAIK a switch/case generates a jump table for each label so it should be faster than if-else-if. esp. since if-else-if does multiple tests (the second if.. )
A switch/case statement is exactly the same as an if/else set. The switch/case was introduced to provide a neater option then a large string of if/else statements. The compiler treats them the same way. You have to make sure you use if...else if...else if...else, this way you drop out of the set as soon as the condition is met.

---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
If your switch is exhausting and uses a small data set, your compiler should generate a jump table.

If it isn''t, it should generate chained if/elses instead.

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
Yeah, switches are probably easier for the compiler to optimise, as it knows that all the following blocks are based around the same condition, and that the condition is only evaluated once. Apart from that they''re generally the same (although an else block is not the same as an else if block, for example).

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]
in c++ the use of switch and if are fundamentally different.
switches use const arguments, and ifs dont.

  switch(y){//not allowed case x: // allowed case NULL: //constant}  


so you may not be able to replace any if/else with a switch, but, switches and ifs can often be implimented interchangably. and imo switches are alot more elegant.

Mark Duffill: switch does a cmp for each case, just like an if/else does.
one thing i dont like about switches is that you cant use pointers, anyone have any idea why they would enforce that?
EvilCrap is the only one to get it right. switch...case statements only evaluate integer expressions, if an if else can be replaced by a switch statement they are a bit speedier

This topic is closed to new replies.

Advertisement