473,473 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

regarding switch-case and if-else statements

Hi Everyone,

I read somewhere that there are some compile time operations behind
switch-case, which is why it can work for cases which evaluates to an
integer or character and not strings and that it makes switch-case
faster than if-else statements, is it true and if so what is the
underlying concept behind a switch-case compilation? Can anyone help in
this regard.

Thanks in advance!!!

Jan 15 '07 #1
5 4732
sa*****@yahoo.co.in wrote:

(almost topical)
I read somewhere that there are some compile time operations behind
switch-case,
Yes. Code-generation happens at compile-time.
which is why it can work for cases which evaluates to an
integer or character and not strings
No. That's just an efficiency-oriented language-design issue.
and that it makes switch-case faster than if-else statements,
What (can) make switch-case faster than if-then chains is that all
the comparisions (can be) made in one go rather than an if at a
time.
is it true and if so what is the underlying concept behind a
switch-case compilation?
A switch-case where the case labels are dense in the range they
cover (so something like 1000..1020 rather than 1, 17, 32,
29, 123, 256, 501, ...) can often be implemented efficiently
by using an indexed jump instruction.

So the compiler (at compile-time) looks to see if that would be
a good way to implement the switch and, if it is, implements
it that way.

Any decent compiler book will discuss this.

--
Chris "by definition ..." Dollin
"He's dead, Jim, but not as we know it." Unsaid /Trek/.

Jan 15 '07 #2
sa*****@yahoo.co.in wrote:
Hi Everyone,

I read somewhere that there are some compile time operations behind
switch-case, which is why it can work for cases which evaluates to an
integer or character and not strings and that it makes switch-case
faster than if-else statements, is it true and if so what is the
underlying concept behind a switch-case compilation? Can anyone help in
this regard.

Thanks in advance!!!
Though the C standard does not consider the relative efficiency of
various constructs, generally for a large number, (say over 10), of
decisions which depend on the value of an integer, which is likely to
be within a reasonable range, the switch statement is likely to
generate faster code than a long series of if-else statements.

This is because, an index table may be used, which means that all the
branches are taken with at most one test. A series of if-else may mean
that to arrive at tests lower down in the chain, all the tests above
have to be gone through, so the very last if/else in the chain will
take the longest to be tested.

Note though that these are implementation dependant details. A compiler
might not generate a jump table for a switch statement and might do so
for an if-else statement. This depends on how good your compiler's
optimisations are.

Jan 15 '07 #3
"santosh" <sa*********@gmail.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
sa*****@yahoo.co.in wrote:
>Hi Everyone,

I read somewhere that there are some compile time operations behind
switch-case, which is why it can work for cases which evaluates to an
integer or character and not strings and that it makes switch-case
faster than if-else statements, is it true and if so what is the
underlying concept behind a switch-case compilation? Can anyone help in
this regard.

Note though that these are implementation dependant details. A compiler
might not generate a jump table for a switch statement and might do so
for an if-else statement. This depends on how good your compiler's
optimisations are.
Two additional notes to the OP:

a)Some compilers I've worked with actually implement a switch as an else-if
below a certain threshold number of cases, usually 3-10. Setting up for a
jump table (in terms of generated code) is not without cost, so a switch()
construct has to be at least a certain size before it might make sense.

b)Note that switch() is different than else-if in that to get to one of the
lower cases in else-if, every test above has to be evaluated. This is not
true with switch().

--
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Jan 15 '07 #4
"David T. Ashley" wrote:
"santosh" <sa*********@gmail.comwrote in message
>sa*****@yahoo.co.in wrote:
>>>
I read somewhere that there are some compile time operations
behind switch-case, which is why it can work for cases which
evaluates to an integer or character and not strings and that it
makes switch-case faster than if-else statements, is it true and
if so what is the underlying concept behind a switch-case
compilation? Can anyone help in this regard.

Note though that these are implementation dependant details. A
compiler might not generate a jump table for a switch statement
and might do so for an if-else statement. This depends on how
good your compiler's optimisations are.

Two additional notes to the OP:

a)Some compilers I've worked with actually implement a switch as
an else-if below a certain threshold number of cases, usually 3-10.
Setting up for a jump table (in terms of generated code) is not
without cost, so a switch() construct has to be at least a certain
size before it might make sense.
While this may happen, the usual criterion is the size of the
prospective jump-table. The if-else may be more efficient than a
sparse table.

switch (i) {
case 1: ...
case 100: ...
case 1000: ...
default: ...
}

could cause an unnecessary monstrous case (jump) table, almost
entirely filled with pointers to the default code.

--
"The power of the Executive to cast a man into prison without
formulating any charge known to the law, and particularly to
deny him the judgement of his peers, is in the highest degree
odious and is the foundation of all totalitarian government
whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
Jan 15 '07 #5
"CBFalconer" <cb********@yahoo.comwrote in message
news:45***************@yahoo.com...
"David T. Ashley" wrote:
>"santosh" <sa*********@gmail.comwrote in message
>>sa*****@yahoo.co.in wrote:

I read somewhere that there are some compile time operations
behind switch-case, which is why it can work for cases which
evaluates to an integer or character and not strings and that it
makes switch-case faster than if-else statements, is it true and
if so what is the underlying concept behind a switch-case
compilation? Can anyone help in this regard.

Note though that these are implementation dependant details. A
compiler might not generate a jump table for a switch statement
and might do so for an if-else statement. This depends on how
good your compiler's optimisations are.

Two additional notes to the OP:

a)Some compilers I've worked with actually implement a switch as
an else-if below a certain threshold number of cases, usually 3-10.
Setting up for a jump table (in terms of generated code) is not
without cost, so a switch() construct has to be at least a certain
size before it might make sense.

While this may happen, the usual criterion is the size of the
prospective jump-table. The if-else may be more efficient than a
sparse table.

switch (i) {
case 1: ...
case 100: ...
case 1000: ...
default: ...
}

could cause an unnecessary monstrous case (jump) table, almost
entirely filled with pointers to the default code.
You may be right, even with the compiler I mentioned. I had all contiguous
case tables, so the compiler's behavior with a sparse table was not
explored.

Phrased differently, for my application, the number of case labels and the
size of the jump table were always the same thing. The more general
behavior was not explored ...

--
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Jan 15 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bryan Parkoff | last post by:
C++ programmers and I tried to experience by writing 65536 switch cases. It is too large for Windows XP and other operating system to handle. It looks like that JMP Table obtains 256K bytes for...
3
by: Mike Wiseley | last post by:
I created a desktop shortcut with "C:\Program Files\Microsoft Office\MSAccess.exe" "C:\My Documents\CopyAToB.mdb" /pwd "password: The trouble is that when running this shortcut, it prompts first...
0
by: Pegaso | last post by:
Hi... I have created a utility library with VC++ 7.1 which is used by a VB 6 client. The library creates a secondary thread of execution in order to listen for connections on a socket. The...
3
by: Sarika | last post by:
I have some questions regarding Modal forms and would appreciate if someone could clarify my understanding. (a) I observed that the modal form's Form_Closing event is generated if I set the...
77
by: M.B | last post by:
Guys, Need some of your opinion on an oft beaten track We have an option of using "goto" in C language, but most testbooks (even K&R) advice against use of it. My personal experience was that...
0
by: Milsnips | last post by:
Hi there, i've created an ASP.NEt (VB) website application and had a few questions regarding the DLL file it creates. 1. Firstly, on occasions when i switch from debug to release, i have...
25
by: v4vijayakumar | last post by:
'continue' within switch actually associated with the outer 'while' loop. Is this behavior protable? int ch = '\n'; while (true) { switch(ch) { case '\n': cout << "test"; continue; } }
4
by: duffdevice | last post by:
Hi, I came across this unexpected behavior while working on something else. I am attempting to return a custom type by value from a global function. I have a trace in the custom class's copy...
11
by: Peter Kirk | last post by:
Hi i have a string variable which can take one of a set of many values. Depending on the value I need to take different (but related) actions. So I have a big if/else-if construct - but what is...
5
by: venkat | last post by:
Hi, I have come across the a pragma definition " #pragma switch direct ". I don't know any thing about pragma's. Even i when i search through google not getting exact information. what does...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.