Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Which of switch statement and if-else statement takes less time to execute?

Reply
Thread Tools

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

 
 
Jack Klein
Guest
Posts: n/a
 
      07-30-2005
On 29 Jul 2005 06:45:40 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in
comp.lang.c:

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


int main(void)
{
switch (1)
{
}

if (0)
{
}
else
{
}
return 0;
}

Each is twice as faster as the other on a DeathStation 9000.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
 
Joe Wright
Guest
Posts: n/a
 
      07-30-2005
(E-Mail Removed) wrote:
> Which of switch statement and if-else statement takes less time to
> execute?
>


That's probably the wrong question. The 'switch(a)/case:' thingy is
limited to multi-way decisions based on the integral value of a. The
'if/else if/else' thingy is much more flexible in that the if conditions
are expressions yielding true or false. I don't use switch() much.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
 
 
 
Peter Pichler
Guest
Posts: n/a
 
      07-31-2005
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

 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      07-31-2005

(E-Mail Removed) 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;

...
}

 
Reply With Quote
 
Bhan
Guest
Posts: n/a
 
      08-01-2005

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


Mark wrote:
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > "Bhan" <(E-Mail Removed)> 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?
>


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-01-2005
*** rude top-posting fixed ***
Bhan wrote:
> Mark wrote:
>> "Keith Thompson" <(E-Mail Removed)> wrote in message
>>> "Bhan" <(E-Mail Removed)> 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 ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      08-01-2005
In article <(E-Mail Removed) .com>,
Bhan <(E-Mail Removed)> wrote:
>
>If you know the answer tell me.


The answer is 42.

>Else keep quite.


Quite what?

 
Reply With Quote
 
Charles Richmond
Guest
Posts: n/a
 
      08-01-2005
CBFalconer wrote:
>
> *** rude top-posting fixed ***
> Bhan wrote:
> > Mark wrote:
> >> "Keith Thompson" <(E-Mail Removed)> wrote in message
> >>> "Bhan" <(E-Mail Removed)> 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 |
+----------------------------------------------------------------+
 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      08-01-2005
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

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      08-01-2005
Bhan wrote:

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



*paaaalonk*



Brian
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
More Efficiency,More Benefit,Less Risk,Less Work! jiajia wu ASP .Net 0 10-01-2009 01:50 PM
More Efficiency,More Benefit,Less Risk,Less Work! lllll Ruby 0 06-08-2009 02:10 PM
More Efficiency,More Benefit,Less Risk,Less Work! 6668 Ruby 0 05-14-2009 12:33 AM
My Switch statement get stuck on the last statement bthumber ASP .Net 5 01-29-2009 07:56 PM
Vista: "Calculating time remaining" takes long time on any fileaction. Even with all suggested KB patches installed Peter Computer Support 2 12-15-2007 12:57 AM



Advertisments