 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gurikar Guest
|
Posted: Fri Apr 29, 2005 12:38 pm Post subject: Switch() vs if else if |
|
|
Whats the difference b/w swicth case and if else if.
Iam finding both are same when you are using in application. Only
difference if else if more flexible to use with all data types. But
some people says switch case is faster than if else if, i dont know is
it and why is it??
Regards
|
|
| Back to top |
|
 |
Sharad Kala Guest
|
Posted: Fri Apr 29, 2005 12:44 pm Post subject: Re: Switch() vs if else if |
|
|
"Gurikar" <msgurikar (AT) gmail (DOT) com> wrote in message
| Quote: | Whats the difference b/w swicth case and if else if.
Iam finding both are same when you are using in application. Only
difference if else if more flexible to use with all data types. But
some people says switch case is faster than if else if, i dont know is
it and why is it??
|
Well, you could read about jump table optimization. But measure and then
only believe what you read.
Sharad
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Fri Apr 29, 2005 12:46 pm Post subject: Re: Switch() vs if else if |
|
|
Gurikar wrote:
| Quote: | Whats the difference b/w swicth case and if else if.
|
The former can only used with integral types, and the case values need to be
compile-time constants. An if/else cascade is likely to be slower than
switch/case.
| Quote: | Iam finding both are same when you are using in application. Only
difference if else if more flexible to use with all data types. But
some people says switch case is faster than if else if, i dont know is
it and why is it??
|
Heh, I wrote the above before I read this. Well, the reason is that
switch/case is often implemented using a jump table with the case values as
index into the table. The if/else is usually implemented using a cascade of
conditional jumps.
|
|
| Back to top |
|
 |
Thomas Matthews Guest
|
Posted: Fri Apr 29, 2005 1:20 pm Post subject: Re: Switch() vs if else if |
|
|
Gurikar wrote:
| Quote: | Whats the difference b/w swicth case and if else if.
How about a jump table, too? |
The switch statement only handles integral quantities.
Compilers may optimize the switch statement into a jump table
(see below).
An if-else-if ladder can handle any type, such as a string.
This construct is more difficult for a compiler to optimize.
A jump table is either a table of addresses (pointers) or
jump instructions. An index is used to access the appropriate
location, then an action is taken. This can be implemented
in C++ using an std::map of <key, function_pointer> or an
array of similar structures.
| Quote: | Iam finding both are same when you are using in application. Only
difference if else if more flexible to use with all data types. But
some people says switch case is faster than if else if, i dont know is
it and why is it??
Only believe "faster than" when actual profiling has been |
performed. And only worry about "faster" when the program
is too slow.
| Quote: |
Regards
The best construct to use is the one that is the easiest |
to understand to the reader.
I prefer to use jump tables, because the data can change
without having to retest the jump-table driver (engine).
All these constructs depend on the situation.
--
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
|
|
| Back to top |
|
 |
msalters Guest
|
Posted: Fri Apr 29, 2005 1:42 pm Post subject: Re: Switch() vs if else if |
|
|
Gurikar wrote:
| Quote: | Whats the difference b/w swicth case and if else if.
Iam finding both are same when you are using in application. Only
difference if else if more flexible to use with all data types. But
some people says switch case is faster than if else if, i dont know
is
it and why is it??
Regards
|
Besides the obvious jumptables, compilers can also generate nested
if-else constructs instead of lineair if-elseif chains. In fact,
these can be mixed.
Asumme you have cases 1..10 and 101..110. Now, a jumptable might be
inconvenient, but implementing such a switch internally as a single
if(x<10) __goto jump[x] else if (x>100&&x<110) __goto jump[x-90]
is certainly legal. Let the compiler deal with those details.
If you would write such code, you'd have to review it every time you
add an enumerator. So does a compiler, but it's a lot faster and
makes less mistakes. Besides, if you did that your source becomes
unreadable. If the compiler does this to your switch, only the
assembly becomes unreadable.
Regards,
Michiel Salters
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|