473,508 Members | 2,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Which of switch statement and if-else statement takes less time to execute?

Which of switch statement and if-else statement takes less time to
execute?

Nov 15 '05 #1
18 3044
sw*******@gmail.com wrote on 29/07/05 :
Which of switch statement and if-else statement takes less time to
execute?


There is no definitve answer to this question. Dd some tests and
measurements in known conditions.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
Nov 15 '05 #2
(supersedes <mn***********************@YOURBRAnoos.fr>)

sw*******@gmail.com wrote on 29/07/05 :
Which of switch statement and if-else statement takes less time to
execute?


There is no definitve answer to this question. Do some tests and
measurements in known conditions.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
Nov 15 '05 #3
I am trying the same.Please help me out
Thanks for your reply
Bhanu

Nov 15 '05 #4
In article <11*********************@g44g2000cwa.googlegroups. com>,
<sw*******@gmail.com> wrote:

Which of switch statement and if-else statement takes less time to
execute?


The difference between them pales in comparison to other
issues such as algorithm choice.

That being said, switch and if-else do different things.

If you have a two-way condition, use if-else.
If you have three or more cases, use switch.

Trying to express a switch as a chain of if-else's is unlikely to be
better than a switch statement. The only time this should be
considered is if you know that one case out of many is statistically
far more likely to occur. Then you might consider testing for that one
case and using switch on the rest.
--
7842++
Nov 15 '05 #5
"Bhan" <sw*******@gmail.com> writes:
I am trying the same.Please help me out
Thanks for your reply
Bhanu


You are trying the same as what? Provide some context. Don't assume
that we can see the article to which you're replying.

And follow the newsgroup a while before posting. If you had done so,
you would have seen the following hundreds of times:

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

--
Keith Thompson (The_Other_Keith) ks***@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.
Nov 15 '05 #6
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Bhan" <sw*******@gmail.com> writes:
I am trying the same.Please help me out
Thanks for your reply
Bhanu


You are trying the same as what? Provide some context. Don't assume
that we can see the article to which you're replying.


He didn't have enough time to provide context...
There was only 5 minutes left for him to complete his online quiz
and he needed the answers quick!

Besides, the question was asked in the message subject... can't you read?
:-)
Nov 15 '05 #7
In article <11*********************@g44g2000cwa.googlegroups. com>,
sw*******@gmail.com wrote:
Which of switch statement and if-else statement takes less time to
execute?


Use a profiler. Try both ways and measure the difference in speed.

If you don't have a profiler, call the function you try to improve
hundred times instead of once only and use a stopwatch. Or call it
thousand times. Then measure.
Nov 15 '05 #8
<sw*******@gmail.com> wrote

Which of switch statement and if-else statement takes less time to
execute?

Just depends.
A very short switch will probaly be compiled to exactly the same code as the
corresponding if ... else. A longer switch may use a jump table, if the
constants in the case are small enough. Usually this will be a little bit
faster than if ... else.

Nov 15 '05 #9
Anonymous 7843 wrote:
Trying to express a switch as a chain of if-else's is unlikely to be
better than a switch statement. The only time this should be
considered is if you know that one case out of many is statistically
far more likely to occur.


....or if the condition may change between tests and you want to take it
into account. I would not recommend it, though. It is hard to debug.

Peter

Nov 15 '05 #10

swaroo...@gmail.com wrote:
Which of switch statement and if-else statement takes less time to
execute?


This is the wrong question to ask. You don't decide to use a switch
over an if-else chain (or vice-versa) based on performance; for one
thing, the performance difference, if any, will not be consistent from
case to case. Secondly, the intent of the code may be obscured by
using an inappropriate construct, making it harder to read and
understand.

Use a switch when you need a multiway branch based on a single integral
value; use an if-else chain for everything else.

For example, writing

switch(a < b)
{
case 1: /* true */
/* do something */
break;

case 0: /* false */
/* do something else */
break;
}

is silly and a little obfuscatory even if it buys you an extra cycle;
writing

if (a < b)
/* do something */
else
/* do something else */

is clearer and more straightforward. By contrast, even though there's
nothing wrong with writing

if (a == 1)
/* do something */
else if (a == 2)
/* do something else */
else if (a == 3)
/* do yet another thing */
....

this is the kind of situation for which the switch statement was
invented in the first place:

switch(a)
{
case 1: /* do something */
break;

case 2: /* do something else */
break;

case 3: /* do yet another thing */
break;

...
}

Nov 15 '05 #11

If you know the answer tell me.
Else keep quite.
Mark wrote:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Bhan" <sw*******@gmail.com> writes:
I am trying the same.Please help me out
Thanks for your reply
Bhanu


You are trying the same as what? Provide some context. Don't assume
that we can see the article to which you're replying.


He didn't have enough time to provide context...
There was only 5 minutes left for him to complete his online quiz
and he needed the answers quick!

Besides, the question was asked in the message subject... can't you read?
:-)


Nov 15 '05 #12
*** rude top-posting fixed ***
Bhan wrote:
Mark wrote:
"Keith Thompson" <ks***@mib.org> wrote in message
"Bhan" <sw*******@gmail.com> writes:

I am trying the same.Please help me out

You are trying the same as what? Provide some context. Don't
assume that we can see the article to which you're replying.


He didn't have enough time to provide context...
There was only 5 minutes left for him to complete his online
quiz and he needed the answers quick!

Besides, the question was asked in the message subject... can't
you read? :-)


If you know the answer tell me.
Else keep quite.


You ARE the rude one. There is still no question to answer.
Message subjects are not necessarily visible when reading a
message.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 15 '05 #13
In article <11**********************@g47g2000cwa.googlegroups .com>,
Bhan <sw*******@gmail.com> wrote:

If you know the answer tell me.
The answer is 42.
Else keep quite.


Quite what?

Nov 15 '05 #14
CBFalconer wrote:

*** rude top-posting fixed ***
Bhan wrote:
Mark wrote:
"Keith Thompson" <ks***@mib.org> wrote in message
"Bhan" <sw*******@gmail.com> writes:

> I am trying the same.Please help me out

You are trying the same as what? Provide some context. Don't
assume that we can see the article to which you're replying.

He didn't have enough time to provide context...
There was only 5 minutes left for him to complete his online
quiz and he needed the answers quick!

Besides, the question was asked in the message subject... can't
you read? :-)


If you know the answer tell me.
Else keep quite.


You ARE the rude one. There is still no question to answer.
Message subjects are not necessarily visible when reading a
message.

I liked the rude guy who told you that you did *not* know anything
about programming. ;-) What a loser!!!

Besides which...the OP's question was implementation dependent and
*not* covered by the C standard.

--
+----------------------------------------------------------------+
| Charles and Francis Richmond It is moral cowardice to leave |
| undone what one perceives right |
| richmond at plano dot net to do. -- Confucius |
+----------------------------------------------------------------+
Nov 15 '05 #15
On Fri, 29 Jul 2005 06:45:40 -0700, swaroophr wrote:
Which of switch statement and if-else statement takes less time to
execute?


That depends on what code your compiler decides to generate for each one
and the environment (e.g. processor model) you are running the code on.
Different compilers can generate different code, the same compiler can
generate different code when given different options. The code the
compilergenerates for these will of course depend on the exact tests being
performed in each case, it can also depend on the code around each
statement. Of course for equivalent if-else and switch statements a
compiler could reasonably generate the same output code.

So there is no general answer to your question. For a particualr
combinatoin of code, compiler, compiler options and execution environment
the only reasonable way of geting an answer is to measure both and compare
them. Even that may not be conclusive, the results can depend enormously
on your input data. E.g. in an if-else chain that is translated fairly
directly by the compiler, if a lot of the time the first test matches it
will be fast, if a lot of the time the last test matches or no tests match
it is likely to be slower.

The best approach is usually to express what you want in the clearest way.
It is very often the case that is clearer to read is also easier for the
compiler to optimise.

Lawrence

Nov 15 '05 #16
Bhan wrote:

If you know the answer tell me.
Else keep quite.

*paaaalonk*

Brian
Nov 15 '05 #17
In article <11**********************@g47g2000cwa.googlegroups .com>,
"Bhan" <sw*******@gmail.com> wrote:
If you know the answer tell me.
Else keep quite.


I think there is an attitude problem here, and it won't get you
anywhere.
Nov 15 '05 #18
Bhan wrote:
If you know the answer tell me.
Else keep quite.


If you can be polite, continue writing...

--
one's freedom stops where other's begin

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 15 '05 #19

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

Similar topics

5
3106
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...
19
15169
by: Christopher Benson-Manica | last post by:
On a note related to my question about logical XOR, I'm trying to think of a non-obfuscated way to condense the following code block (which, as usual, I did not write): switch( int_val_1 ) {...
10
1964
by: Eric | last post by:
I have a pet project I'm working on. I start in DOS in assembly, switch to protected mode and play around with things in that environment. I'm wondering if i started at main in a C program then...
7
10139
by: Christopher Benson-Manica | last post by:
Can you goto switch labels? int i=0; /* arbitrary */ switch( i ) { case 0: if( !some_validity_check() ) { goto error; /* could be default as well */ } /* proceed normally */
9
1554
by: Robert | last post by:
#include <stdio.h> int main() { switch (29) { case 29: int msglen = 22; printf("Hello\n"); }; return 0; }
13
7406
by: William Stacey | last post by:
Using the following code sample: public byte Get() { // <= Possible to switch Here?? lock(syncLock) { //Do something in Get(). } }
5
3669
by: Nadav | last post by:
Hi, I am using FileStream's Async API: BeginRead/EndRead, upon completion callback execution I use the read data and call EndRead, Taking that in mind, I Wonder... does calling EndRead will cause...
7
3332
by: Chad | last post by:
The program: #include <stdio.h> void hello(void) { printf("hello \n"); } void hi(void) { printf("hi \n");
6
4521
by: asit | last post by:
please modify to get the correct output.(switch case is compulsory) #include <stdio.h> int main() { char ch; printf("Enter any character : "); ch=getch(); switch(ch)
23
1914
by: Martin T. | last post by:
Hi all! char* p = new char; // POD! .... delete p; // std compliant delete p; // will work on VC8 free(p); // will also work on VC8 I am interested on which platforms/compilers the...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7133
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...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7504
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...
1
5059
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
3214
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
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.