469,619 Members | 1,963 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,619 developers. It's quick & easy.

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

Jul 23 '05 #1
4 6698
Gurikar wrote:
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.
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.

Jul 23 '05 #2

"Gurikar" <ms*******@gmail.com> wrote in message
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

Jul 23 '05 #3
Gurikar wrote:
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.

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.


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
Jul 23 '05 #4

Gurikar wrote:
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

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Mike and Jo | last post: by
10 posts views Thread by clueless_google | last post: by
17 posts views Thread by prafulla | last post: by
11 posts views Thread by hasadh | last post: by
13 posts views Thread by Michael Griebe | last post: by
10 posts views Thread by Evie | last post: by
5 posts views Thread by sam_cit | last post: by
13 posts views Thread by Satya | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.