By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,656 Members | 800 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,656 IT Pros & Developers. It's quick & easy.

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

P: n/a
Which is better using a switch statement or the if-then equivalent
of switch ?

Nov 14 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
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.

Nov 14 '05 #2

P: n/a
ju**********@yahoo.co.in wrote:
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #3

P: n/a

Michael Mair wrote:
ju**********@yahoo.co.in wrote:
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.


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

Nov 14 '05 #4

P: n/a
On 16 May 2005 06:06:11 -0700, ju**********@yahoo.co.in
<ju**********@yahoo.co.in> wrote:
Michael Mair wrote:
ju**********@yahoo.co.in wrote:
> 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.


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
Nov 14 '05 #5

P: n/a
>> 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.


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

Nov 14 '05 #6

P: n/a
On 16 May 2005 06:06:11 -0700, in comp.lang.c ,
ju**********@yahoo.co.in wrote:

Michael Mair wrote:
ju**********@yahoo.co.in wrote:
> 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.


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


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.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #7

P: n/a
ju**********@yahoo.co.in writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8

P: n/a
ju**********@yahoo.co.in writes:
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.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
Nov 14 '05 #9

P: n/a
ju**********@yahoo.co.in 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'.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
One of the drawbacks of being a martyr is that you have to die.
Nov 14 '05 #10

P: n/a
ju**********@yahoo.co.in wrote:
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
Nov 14 '05 #11

P: n/a
Thomas Matthews <Th*************************@sbcglobal.net> writes:
ju**********@yahoo.co.in wrote:
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.


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>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #12

P: n/a
In article <11*********************@g44g2000cwa.googlegroups. com>,
ju**********@yahoo.co.in wrote:
Michael Mair wrote:
ju**********@yahoo.co.in wrote:
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.


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.
Nov 14 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.