Re: switch vs if

From: Erwin S. Andreasen (erwin@PIP.DKNET.DK)
Date: 05/03/98


On Sun, 3 May 1998, Chris Powell wrote:

> WHich is quicker
> if (this == that || this == that2)
> or
> switch (this) {
>   case that:
>   case that2:

It's probably a miniscule difference - it is not something you should
worry about unless you find out that your program DOES run too slow.
Premature optimization can end up giving clarity. If it's only 2-3 items,
the if might be nicer, especially if it is the same code that is to be
executed in all cases.

With gcc, -O2, the following code (Intel machine)

int test1 (int a)
{
        if (a == 2 || a == 3)
                return 1;
}

int test2 (int a)
{
        switch(a)
        {
                case 2:
                case 3:

                return 1;
        }
}

08048538 <test1>:
 8048538:       55              pushl  %ebp
 8048539:       89 e5           movl   %esp,%ebp
 804853b:       8b 45 08        movl   0x8(%ebp),%eax

 804853e:       83 c0 fe        addl   $0xfffffffe,%eax
 8048541:       83 f8 01        cmpl   $0x1,%eax
 8048544:       77 05           ja     804854b <test1+13>
 8048546:       b8 01 00 00 00  movl   $0x1,%eax
 804854b:       89 ec           movl   %ebp,%esp
 804854d:       5d              popl   %ebp
 804854e:       c3              ret
 804854f:       90              nop

08048550 <test2>:
 8048550:       55              pushl  %ebp
 8048551:       89 e5           movl   %esp,%ebp
 8048553:       8b 45 08        movl   0x8(%ebp),%eax

 8048556:       83 f8 03        cmpl   $0x3,%eax
 8048559:       7f 0a           jg     8048565 <test2+15>
 804855b:       83 f8 02        cmpl   $0x2,%eax
 804855e:       7c 05           jl     8048565 <test2+15>
 8048560:       b8 01 00 00 00  movl   $0x1,%eax
 8048565:       89 ec           movl   %ebp,%esp
 8048567:       5d              popl   %ebp
 8048568:       c3              ret
 8048569:       8d 76 00        leal   0x0(%esi),%esi


Yep, I suppose the assembly code speaks for itself :P

In this case, it seems the if is reduced to just one compare and jump,
while the switch has two compares and jumps. That will be about one
microsecond's difference :)

The time wasted by disassembling your code like this however, is I think
far more than it taken if you just pick the code that is more legible.
I.e. if you have 6 different cases, a switch might look better, If you
want some of the cases to drop through, a switch is much clearer. If you
have just 2-3 different choices an if might be enough.

But usually, trying to optimize for the compiler does not do much good
(e.g. doing var >>=2 rather than var /= 4). Things that the compiler is
less likely to get right that you might optmize for are something like
moving constant parts out of loops, e.g.:

const char *string = "some value that will not change";

for (int i = 0; i < 10000; i++)
        number += strlen (string) * 42;


And more importantly, algorithms.

 =============================================================================
Erwin Andreasen   Herlev, Denmark <erwin@pip.dknet.dk>  UNIX System Programmer
<URL:http://www.abandoned.org/drylock/>     <*>         (not speaking for) DDE
 =============================================================================


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST