which is better "switch" or "if-else"

J

junky_fellow

Which is better using a switch statement or the if-then equivalent
of switch ?
 
J

Jaspreet

Hi

It depends on the the conditions. If you just have int or char
constants as the value of the expression then switch statement is
concise and easy to read.

However if you have comples values and/or multiple ranges then if
condition is better.

If you give us an example of your problem then possibly someone could
tell you which is betetr to use.
 
M

Michael Mair

Which is better using a switch statement or the if-then equivalent
of switch ?

Define "better".
You can do things with switch statements and fallthrough cases that
are awkward with if/(else if/)else blocks. On the other hand,
switch works only for integers and accepts only constants for the
case labels.

As remarked in another reply: Tell us what you want to achieve
and we can comment in more detail on the pros and contras.

Cheers
Michael
 
J

junky_fellow

Michael said:
Define "better".
You can do things with switch statements and fallthrough cases that
are awkward with if/(else if/)else blocks. On the other hand,
switch works only for integers and accepts only constants for the
case labels.

As remarked in another reply: Tell us what you want to achieve
and we can comment in more detail on the pros and contras.

I read somewhere that assembly of switch generates jump tables
and is faster as compared to if-then equivalent.
 
C

Chris Croughton

I read somewhere that assembly of switch generates jump tables
and is faster as compared to if-then equivalent.

It may, it may not. It depends on your compiler (not just the make,
different versions of the same compiler may generate different code),
the optimisations selected, the capabilities of your hardware, the
number and values of the case constants, and probably a load of other
things.

The same is true for most other "which is faster/smaller" questions, the
answer is "it depends". If you really want to know, for your specific
system, write programs to test it...

(I wrote an optimiser for MSDOS 'TSR' programs once which attempted to
solve the "small as possible" problem for switches. Then I found that
it was different on a 286 to an 8086...)

Chris C
 
G

Gordon Burditt

As remarked in another reply: Tell us what you want to achieve
I read somewhere that assembly of switch generates jump tables
and is faster as compared to if-then equivalent.

Any statement of the form "A is faster than or slower than or about the
same speed as B" is probably false if you don't specify the hardware,
compiler and version, and OS and version.

Maybe the compiler generates a jump table. I doubt it would in this case:

switch(fark) {
case 0x7fffffff: ...; break;
case 0: ...; break;
case 0x40003726: ...; break;
case 0x80000007: ...; break;
default: ...; break;

since the jump table would likely exceed available address space on
a machine with 32-bit integers and a 32-bit address space.

Compilers can use various strategies: sequential if-then-else,
jump table, or binary search (nested if-then-else), and they can
use different strategies for parts of the search space. For example,
if you have a switch with 20 different values in the range 'a' ..
'z', plus EOF, it could do the EOF with if-then-else, and use a
jump table for the rest of it where the values are densely packed
together. A jump table (with initial range checking) probably isn't
worth it if the switch has few cases, e.g. less than 3 - 10.

Gordon L. Burditt
 
M

Mark McIntyre

Define "better".
You can do things with switch statements and fallthrough cases that
are awkward with if/(else if/)else blocks. On the other hand,
switch works only for integers and accepts only constants for the
case labels.

As remarked in another reply: Tell us what you want to achieve
and we can comment in more detail on the pros and contras.

I read somewhere that assembly of switch generates jump tables
and is faster as compared to if-then equivalent.[/QUOTE]

maybe. Maybe not. This is entirely dependent on your hardware and how
good your compiler is at optimising. Learn the three rules of
optimisation before proceeding.
 
K

Keith Thompson

Which is better using a switch statement or the if-then equivalent
of switch ?

The switch statement has restrictions that if-then doesn't. A switch
statement compares a single integer value to a number of compile-time
constant values; an if-then can evaluate any arbitrary condition.

As a matter of style, if you *can* use a switch, you probably should
use the switch rather than the equivalent if-then-else chain -- unless
there are only one or two choices.

If you're concerned about performance, don't be. Any decent compiler
should generate good code for any switch or if-then-else statement.
 
B

Ben Pfaff

Which is better using a switch statement or the if-then equivalent
of switch ?

The better choice is the one that results in code that is easier
to read.
 
S

SM Ryan

(e-mail address removed) wrote:
# Which is better using a switch statement or the if-then equivalent
# of switch ?

Which is easier to understand in your code?

As far as which is more efficient, simply by posting this question, you have
already used up more cpu cycles and burnt out more electrons than your
program would ever save by being more 'efficient'.
 
T

Thomas Matthews

Which is better using a switch statement or the if-then equivalent
of switch ?

Have you tried implementing a "jump table"?
A jump table is a array of <key, function pointer>
records. If the keys are contiguous, the table
becomes an array of function pointers.

If you want to force the compiler to use a jump
table, then make one yourself.

I use if-then-elseif ladders for small quantities.
For bigger quantities, I use a switch statment.
For strings and data driven processes, I use a
jump table. Each has their advantages and
disadvantages; you'll just have to learn when
to use them.

--
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.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
K

Keith Thompson

Thomas Matthews said:
Have you tried implementing a "jump table"?
A jump table is a array of <key, function pointer>
records. If the keys are contiguous, the table
becomes an array of function pointers.

If you want to force the compiler to use a jump
table, then make one yourself.

I use if-then-elseif ladders for small quantities.
For bigger quantities, I use a switch statment.
For strings and data driven processes, I use a
jump table. Each has their advantages and
disadvantages; you'll just have to learn when
to use them.

A jump table used (by the compiler) to implement a switch statement is
likely to be more efficient than an explicit one using function
pointers. A compiler-generated jump table will be a table of code
addresses (labels), not function pointers, avoiding the overhead of a
function call and return. There's no good way to implement an array
of labels in C. <OT>gcc has an extension that lets you store the
value of a label in a pointer, and use the pointer as the target of a
goto; this is, of course, non-portable.</OT>
 
C

Christian Bau

I read somewhere that assembly of switch generates jump tables
and is faster as compared to if-then equivalent.

Don't trust books. The good ones were right when they were written, but
they are not right anymore. The bad ones were never right in the first
place.

If you want to know what is faster, then measure.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top