Which is better using a switch statement or the if-then equivalent
of switch ?
|
|
0
|
|
|
|
Reply
|
junky_fellow (377)
|
5/16/2005 11:55:55 AM |
|
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.
|
|
0
|
|
|
|
Reply
|
jsingh.oberoi (216)
|
5/16/2005 12:20:18 PM
|
|
junky_fellow@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.
|
|
0
|
|
|
|
Reply
|
Michael.Mair (1492)
|
5/16/2005 12:49:00 PM
|
|
Michael Mair wrote:
> junky_fellow@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.
|
|
0
|
|
|
|
Reply
|
junky_fellow (377)
|
5/16/2005 1:06:11 PM
|
|
On 16 May 2005 06:06:11 -0700, junky_fellow@yahoo.co.in
<junky_fellow@yahoo.co.in> wrote:
> Michael Mair wrote:
>> junky_fellow@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
|
|
0
|
|
|
|
Reply
|
chris23 (644)
|
5/16/2005 2:11:04 PM
|
|
>> 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
|
|
0
|
|
|
|
Reply
|
gordonb.y1l1a (1)
|
5/16/2005 5:30:54 PM
|
|
On 16 May 2005 06:06:11 -0700, in comp.lang.c ,
junky_fellow@yahoo.co.in wrote:
>
>Michael Mair wrote:
>> junky_fellow@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>
|
|
0
|
|
|
|
Reply
|
markmcintyre (4547)
|
5/16/2005 8:22:26 PM
|
|
junky_fellow@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) kst-u@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.
|
|
0
|
|
|
|
Reply
|
kst-u (21474)
|
5/16/2005 8:29:46 PM
|
|
junky_fellow@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: blp@cs.stanford.edu
web: http://benpfaff.org
|
|
0
|
|
|
|
Reply
|
blp (3953)
|
5/17/2005 12:01:38 AM
|
|
junky_fellow@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.
|
|
0
|
|
|
|
Reply
|
wyrmwif (945)
|
5/17/2005 5:17:07 AM
|
|
junky_fellow@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
|
|
0
|
|
|
|
Reply
|
Thomas_MatthewsSpamBotsSuck (408)
|
5/17/2005 3:20:21 PM
|
|
Thomas Matthews <Thomas_MatthewsSpamBotsSuck@sbcglobal.net> writes:
> junky_fellow@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) kst-u@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.
|
|
0
|
|
|
|
Reply
|
kst-u (21474)
|
5/17/2005 8:19:04 PM
|
|
In article <1116248771.390860.42360@g44g2000cwa.googlegroups.com>,
junky_fellow@yahoo.co.in wrote:
> Michael Mair wrote:
> > junky_fellow@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.
|
|
0
|
|
|
|
Reply
|
christian.bau (880)
|
5/17/2005 9:34:29 PM
|
|
|
12 Replies
31 Views
(page loaded in 0.14 seconds)
Similiar Articles: \if else function to switch between Beamer and [handout]Beamer ...... build up to something, for explanatory purposes, but would be better ... if-else-end in anonymous functions - comp.soft-sys.matlab ... \if else function to switch ... peek() vs unget(): which is better? - comp.lang.c++.moderated ...... std::isspace(c)) { } if (!is) { // end of input } else if ... char array with null chars - comp.lang.python ... peek() vs unget(): which is better ... Random walk across array - comp.lang.c... array[--x][y] = ++c; } else { /* Otherwise ... things simple in the early going and will describe better ... This situation simply cries out for a `switch ... vhdl code for debouncing push button - comp.arch.fpgaThe best switch to debounce is a double throw switch. The FF stays in a given state ... max := xxxxx; if reset then timer_count = 0; pb_debounced = '0'; else ... Switch + Case - comp.lang.asm.x86I don't like to write switch keyword and ... C++ definitely will optimize > switch statements to jump tables, or better. ... Structures Tutorial: if, if-else, and switch-case ... orange light is blinking on port connected to the router - comp ...Or something else can i do from my side?? Actually the switch is configured to have only two VLAN. That's it nothing much complex configuration is used. lcc or gcc ,which better? - comp.compilers.lccIt would be better if you explained what is "a specific MCU". Not everyone understands ... I have not used anytning else, apart from gcc in Linux. Regards, Bill Freeman MLPPP vs. Cisco CEF - comp.dcom.sys.cisco... several T1's to the same provider to create say 4xT1 6mbps pipe is it better to go ... take a big jump, up to the 7600 series: everthing in between is classed as a "switch ... Re: Creating one report with columns based on different subsets ...... type of thing on (and we do this very often which is why I'm looking for a better ... data=fortabul ; class switch_to scope new ; format new new. ; table switch_to all ... Input data dependency violation due to action subsystems. See ...May be changes to the connection between SS and switch may fix the issue. If/else can be done with switch blocks as well - although not so efficiently as with action ... MSP 1+1 unidiraectional and ALS - comp.dcom.sdh-sonet... single fiber having problem), it will switch to secondary.At the same time no switch ... Always remember that you are unique...just like everyone else... Switching to Form or list - comp.databases.filemakerHey could some tell me why sometimes a small script works for switching to Form View to List View works and sometimes it doesn't switch Show All Rec... Managed/unmanaged Switch - comp.unix.solaris... networks, can someone explain the difference between a managed and unmanaged switch. ... they should be able to drive a 100 mbit ethernet at 10 Mbytes/second or better ... how to check if an object has already been created - comp.lang ...*/ } Unless someone else has a better way, that's how I'd do it. Hope that helps. ... else { > /* ... do other stuff ... */ > } > > Unless someone else has a better way ... Vulnerability scan for routers and switches - comp.dcom.sys.cisco ...Is there a better way out ? I am thinking of a list to be taken by Nessus as an input ... We have 5 remote sites, each with a 2621XM router and a 24 or 48 port Cisco switch ... If-else vs switch – Which is better? | Thread.currentThread().join()Use switch instead of if-else, its more readable and has better performance. I have to admit that this was one of my favorite code review comment. Until one Speed Test: Switch vs If-Else-If (Page 1 of 2) :: BlackWasp ...Test Description Purpose. This test was designed to compare the execution time of the switch statement and the if-else-if ladder code structure when used to select a ... 7/27/2012 6:14:16 PM
|