switch versus if [C++]

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

switch versus if [C++]

Post by arras »

Are there any difference between switch() and if() in terms of effectivity?

For example:

Code: Select all

if(a == 0) ...do something;
else
if(a == 1) ...do something;
else ...do something;

Code: Select all

switch(a)
{
   case 0: ...do something; break;
   case 1: ...do something; break;
   default: ...do something;
}
Last edited by arras on Sat Apr 05, 2008 7:50 pm, edited 1 time in total.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Yes, the if cascade will do the test in each cycle, the switch will only test once and jump directly into the proper case statement.
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

OK, thanks hybrid.
Kriolyth
Posts: 26
Joined: Wed Mar 26, 2008 6:59 pm
Location: Moscow, Russia

Post by Kriolyth »

"switch" under most compilers will generate a jump-table, so after evaluation it will take only one CPU instruction to get to the needed branch.
"if" is hardly optimized at all, so there is a huge chance that every if-condition will be tested.
(actually repeating hybrid)
The cake is a lie.
sp00n
Posts: 114
Joined: Wed Sep 13, 2006 9:39 am

Post by sp00n »

As i remember a standard asm code by M.Abrash when he was discovering "if vs switch" there was a result: use "if ... else" statement only if you have less than 4 conditions (max example: if ... else if ...), in other cases use a switch :) ('cause 3 jumps better than jump-table for 3 cases, but j-table for 4 cases better than 4 if-jumps ) :)
But in modern CPU's it itsn't critical problem, i think)
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Thanks mates...
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

If we're already talking about switches, it's the same syntax for Java right?
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Also, it is best to eliminate as many branching conditions as you can in your code. Even with branch hinting, if the code is in a heavily used loop, it may create for a slowdown or bottleneck. (If too many are present.)

To make what I'm saying a little bit easier, say we have a conditional 'jpz' (jump zero) instruction. If the test case is true it executes in 7 cycles, but if the test case is not true it takes 12 cycles to execute.
TheQuestion = 2B || !2B
Swarmer
Posts: 100
Joined: Mon Apr 16, 2007 7:23 am

Post by Swarmer »

Switches are also much cleaner and easier to read in my opinion, which is just as important.
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

What about this code?:

Code: Select all

void somefunction(int a, int b, int c)
{
   if(a == 10)
   {
      a = 20;
      return;
   }
   
   if(b == 4)
   {
      b = 0;
      return;
   }
   
   if(c < 1)
   {
      c = 1;
      return;
   }
}
compared to:

Code: Select all

void someotherfunction(int a, int b, int c)
{
   if(a == NULL)
   {
      a = 20;
   }
   else
   if(b == 4)
   {
      b = 0;
   }
   else
   if(c < 1)
   {
      c = 1;
   }
}
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

That function has no external side effects and no return, so it will be the same. Both implementations will end up being an empty function.

If you are actually curious about this kind of thing, find out what compile flag you need to use to generate assembly, then use it. You really have to test for yourself because the answers will depend on the code, the optimization level, and the compiler.

Travis
wyrmmage
Posts: 204
Joined: Sun Mar 16, 2008 3:12 am
Contact:

Post by wyrmmage »

Swarmer wrote:Switches are also much cleaner and easier to read in my opinion, which is just as important.
yep; the other big advantage to them is that if you change the name of the variable (or you put it inside of an object), you only have to change one line of code versus quite a few. I find myself doing this quite often when I'm first setting up a game :)

-wyrmmage
d3jake
Posts: 198
Joined: Sat Mar 22, 2008 7:49 pm
Location: United States of America

Post by d3jake »

Would there be a way to adapt the switch statement to your movement code in your free flight demo?
(This code if nobody has any clue what I'm talking about)

Code: Select all

    // direction control
        if(keys[irr::KEY_LEFT])
        {
            turn(shuttle, 0.25);
        }
        if(keys[irr::KEY_RIGHT])
        {
            turn(shuttle, -0.25);
        }
        if(keys[irr::KEY_UP])
        {
            pitch(shuttle, -0.25);
        }
        if(keys[irr::KEY_DOWN])
        {
            pitch(shuttle, 0.25);
        }
        if(keys[irr::KEY_COMMA])
        {
            roll(shuttle, 0.45);
        }
       // And so on...
The Open Descent Foundation is always looking for programmers! http://www.odf-online.org
"I'll find out if what I deleted was vital here shortly..." -d3jake
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

No there is no particular, easy, way to do that, and you wouldn't want to do that. That code is engineered that way so as to allow multiple key presses from a boolean array. Thus the user can press up and left, and it will result in an up and left movement.
TheQuestion = 2B || !2B
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Thanks once again for answers.
That code is engineered that way so as to allow multiple key presses from a boolean array. Thus the user can press up and left, and it will result in an up and left movement.
Exactly. Also user input is not going to happen much too often (maximum once per loop). You do not have to struggle for such subtle optimization. You need that in parts of code which are going to be called say few dozens or hundred times per loop.
Post Reply