Switch() vs if else if

G

Gurikar

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
 
S

Sharad Kala

Gurikar said:
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
 
R

Rolf Magnus

Gurikar said:
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.
 
T

Thomas Matthews

Gurikar said:
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.

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
 
M

msalters

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

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top