Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Which of switch statement and if-else statement takes less time to execute? (http://www.velocityreviews.com/forums/t438912-which-of-switch-statement-and-if-else-statement-takes-less-time-to-execute.html)

swaroophr@gmail.com 07-29-2005 01:45 PM

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?


Emmanuel Delahaye 07-29-2005 01:47 PM

Re: Which of switch statement and if-else statement takes less time to execute?
 
swaroophr@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"



Emmanuel Delahaye 07-29-2005 01:49 PM

Re: Which of switch statement and if-else statement takes less time to execute?
 
(supersedes <mn.ebb37d57027ee3c6.15512@YOURBRAnoos.fr>)

swaroophr@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



Bhan 07-29-2005 01:51 PM

Re: Which of switch statement and if-else statement takes less time to execute?
 
I am trying the same.Please help me out
Thanks for your reply
Bhanu


Anonymous 7843 07-29-2005 06:35 PM

Re: Which of switch statement and if-else statement takes less time to execute?
 
In article <1122644740.596912.97800@g44g2000cwa.googlegroups. com>,
<swaroophr@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++

Keith Thompson 07-29-2005 08:06 PM

Re: Which of switch statement and if-else statement takes less timeto execute?
 
"Bhan" <swaroophr@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) kst-u@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.

Mark 07-29-2005 08:59 PM

Re: Which of switch statement and if-else statement takes less time to execute?
 
"Keith Thompson" <kst-u@mib.org> wrote in message
news:ln7jf9gyw1.fsf@nuthaus.mib.org...
> "Bhan" <swaroophr@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?
:-)



Christian Bau 07-29-2005 10:41 PM

Re: Which of switch statement and if-else statement takes less time to execute?
 
In article <1122644740.596912.97800@g44g2000cwa.googlegroups. com>,
swaroophr@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.

Malcolm 07-30-2005 08:36 AM

Re: Which of switch statement and if-else statement takes less time to execute?
 
<swaroophr@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.




Neo 07-30-2005 01:03 PM

Re: Which of switch statement and if-else statement takes less time to execute?
 
When the decisions to be made are huge
like 1000x transactions you prefer switch which is efficient and fast
because of its random ness in accessing the content

when the decisions are too less like 10 or so even if the flow goes
through many ifs and else checks its not going to make much a
difference.

so depending on the number of checks
at one point your compiler will take a decision to give out the same
obj code for both .if U manage to use Optimisation.

But giving out an answer like ... which among Switch and If else is
quick
is as meaning less as .. what fills your stomach soon Breakfast or
Lunch ??
It depends on you .and the cook



All times are GMT. The time now is 01:18 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.