Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > switch vs. if

Reply
Thread Tools

switch vs. if

 
 
Denis Remezov
Guest
Posts: n/a
 
      06-10-2004
Dave wrote:
>
> Hello all,
>
> Consider the case of an if with many else if clauses vs. a switch with many
> cases.
>
> In thinking about how such control constructs would likely be implemented,
> it would seem that neither would be superior to the other in terms of
> execution speed of the generated code.
>
> Would others agree that this is true for "typical" implementations?
>
> Thanks,
> Dave


For some reason I always expected the compilers to optimise a long switch
statement by using binary search. Now, the interesting part is that
gcc 3.3.3 appears to do exactly that (O(log n)), unless the range of values
is compact, in which case it uses a table (O(const)).
The if statement, on the other hand, in my tests was implemented literally,
without any clever optimisation.

For long selection lists the difference can be measurable. Though it comforts
me to think of it as of a potential bonus, I'd pick a switch over if whenever
appropriate regardless of the performance.

Denis
 
Reply With Quote
 
 
 
 
Dave
Guest
Posts: n/a
 
      06-10-2004
Hello all,

Consider the case of an if with many else if clauses vs. a switch with many
cases.

In thinking about how such control constructs would likely be implemented,
it would seem that neither would be superior to the other in terms of
execution speed of the generated code.

Would others agree that this is true for "typical" implementations?

Thanks,
Dave


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-10-2004
Dave wrote:
> Consider the case of an if with many else if clauses vs. a switch with many
> cases.
>
> In thinking about how such control constructs would likely be implemented,
> it would seem that neither would be superior to the other in terms of
> execution speed of the generated code.
>
> Would others agree that this is true for "typical" implementations?


It is probably true. I've never written an optimizing C++ compiler, so
I can't say I know what's involved. But it does seem that a 'switch'
statement with its very direct and straightforward approach would be
actually easier to implement as a table than an 'if' statement. Besides,
the 'switch' cannot really be screwed up by sudden introduction of some
arbitrary expression (during code maintenance, I mean).

V
 
Reply With Quote
 
Jimmy_B
Guest
Posts: n/a
 
      06-11-2004
Dave wrote:
> Consider the case of an if with many else if clauses vs. a switch with many
> cases.
>
> In thinking about how such control constructs would likely be implemented,
> it would seem that neither would be superior to the other in terms of
> execution speed of the generated code.
>
> Would others agree that this is true for "typical" implementations?


No. Any half-decent compiler will reduce a switch statement to a jump
table, which is O(1) to the number of cases (assuming the cases are
sequential). A series of if-else statements will be reduced only by an
exceedingly clever compiler, and if compiled in the usual way will be O(n)
to the number of cases. An if statement will produce code with size
proportional to the number of cases, whereas a switch statement may produce
a very large mostly-empty table if the cases are sparse (depending on your
compiler.)

In fact, a switch statement is conceptually much more like a jump table
than like a series of if-else statements. Consider Duff's Device, for example:
switch(count%{
case 0: do{ *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
}while(--n>0);
}

--
CalcRogue: TI-89 and TI-92+. <http://www.gis.net/~wssddc/jimmy_b/>.
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      06-11-2004
"Dave" <> wrote in message
news:...
> Hello all,
>
> Consider the case of an if with many else if clauses vs. a switch with

many
> cases.
>
> In thinking about how such control constructs would likely be implemented,
> it would seem that neither would be superior to the other in terms of
> execution speed of the generated code.
>
> Would others agree that this is true for "typical" implementations?


I advise not worrying about 'performance', but to write
the code to be as clear and readable as possible. Make
the code maintainable. Programmer time is far more
expensive than computer time.

IMO two or three conditions would probably be best served
with 'if' and 'else', whereas more conditions than than would
probably be most clearly expressed with a 'switch'. And
if the number of conditions becomes excessive, consider
e.g. a table of function pointers (perhaps stored in a
std::map).

Only concern yourself with optimization after you've
empirically proven that the code's performance is
unacceptable. If that is the case, then use a profiler
to find out where the slowdowns really are (they're
frequently not where you'd think). An improvement in
performance is more typically gained by optimizing
the 'higher level' algorithm(s) than messing around
with 'micro-optimizations', which imo is the job of
the compiler.

-Mike


 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      06-11-2004
"Mike Wahler" <> wrote in message news:5s8yc.1154

> I advise not worrying about 'performance', but to write
> the code to be as clear and readable as possible. Make
> the code maintainable. Programmer time is far more
> expensive than computer time.


Usually, switch case looks better to me, but I've gotten used to both ways.
For complex cases, it's best to use the command pattern, where there are
many objects and each implements a virtual function defined as pure virtual
in the base class.


> IMO two or three conditions would probably be best served
> with 'if' and 'else', whereas more conditions than than would
> probably be most clearly expressed with a 'switch'. And
> if the number of conditions becomes excessive, consider
> e.g. a table of function pointers (perhaps stored in a
> std::map).


Right, or the command pattern.


> Only concern yourself with optimization after you've
> empirically proven that the code's performance is
> unacceptable. If that is the case, then use a profiler
> to find out where the slowdowns really are (they're
> frequently not where you'd think). An improvement in
> performance is more typically gained by optimizing
> the 'higher level' algorithm(s) than messing around
> with 'micro-optimizations', which imo is the job of
> the compiler.




 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      06-11-2004
Denis Remezov wrote:

> Dave wrote:
>
>>Hello all,
>>
>>Consider the case of an if with many else if clauses vs. a switch with many
>>cases.
>>
>>In thinking about how such control constructs would likely be implemented,
>>it would seem that neither would be superior to the other in terms of
>>execution speed of the generated code.
>>
>>Would others agree that this is true for "typical" implementations?
>>
>>Thanks,
>>Dave

>
>
> For some reason I always expected the compilers to optimise a long switch
> statement by using binary search. Now, the interesting part is that
> gcc 3.3.3 appears to do exactly that (O(log n)), unless the range of values
> is compact, in which case it uses a table (O(const)).
> The if statement, on the other hand, in my tests was implemented literally,
> without any clever optimisation.
>
> For long selection lists the difference can be measurable. Though it comforts
> me to think of it as of a potential bonus, I'd pick a switch over if whenever
> appropriate regardless of the performance.
>
> Denis


Zilog's old Z8000 compilers took advantage of the instruction set and generated
a jump table even for noncontiguous values.
 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      06-11-2004
On Thu, 10 Jun 2004 16:07:48 -0700, "Dave" <>
wrote:

>Hello all,
>
>Consider the case of an if with many else if clauses vs. a switch with many
>cases.
>
>In thinking about how such control constructs would likely be implemented,
>it would seem that neither would be superior to the other in terms of
>execution speed of the generated code.


Not true. The switch expression need be evaluated only once, whereas
each if expression must be independently evaluated (or at least enough
of it to return its Boolean result).

>Would others agree that this is true for "typical" implementations?


I suppose one could generate an assembly listing from a "typical"
compilation and examine the way the compiler has done it. "Typical"
doesn't necessarily mean anything, does it?

As someone else pointed out, you can usually do away with both if you
have properly designed your classes with virtual functions.


--
Bob Hairgrove

 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      06-11-2004
Dave wrote:
> Hello all,
>
> Consider the case of an if with many else if clauses vs. a switch with many
> cases.
>
> In thinking about how such control constructs would likely be implemented,
> it would seem that neither would be superior to the other in terms of
> execution speed of the generated code.
>
> Would others agree that this is true for "typical" implementations?
>
> Thanks,
> Dave
>
>


Hmmm, remember the third alternative. (I.e. most of the timer there
is always a third alternative, not just black or white.)

One can also implement a jump table (a table of function pointers),
instead of the above. On benefit to the table drive approach is
that the executable code doesn't need to be retested; the data
is changed not the driver. On the other hand, there are cases
where the table can be used as premature optimization (in which
your wasting programming time for a construct that the compiler
will automatically generate).

I've just learned, the hard way, about converting all switch
statements into function tables. Sometimes, switch statements
can be a lot easier to read than a function table.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trunking between Cisco Catalyst 500 switch and other type of switch jshubo@yahoo.com Cisco 3 01-19-2008 12:10 AM
Switch/Switch problem fibre gigbit ethernet hartmut.albers@gmail.com Cisco 2 09-06-2005 04:59 PM
Is a frame switch and an ISDN switch really needed? owmanstubbedmytoe Cisco 2 12-05-2004 07:15 AM
bridge / layer 2 switch / layer 3 switch Joel M. Baldwin Cisco 2 11-06-2003 11:19 PM
difference b/w layer 2 switch and layer 3 switch praveen Cisco 1 10-22-2003 07:19 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57