Velocity Reviews > 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?

swaroophr@gmail.com
Guest
Posts: n/a

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

Emmanuel Delahaye
Guest
Posts: n/a

 07-29-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
Guest
Posts: n/a

 07-29-2005
(supersedes <(E-Mail Removed)>)

(E-Mail Removed) 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
Guest
Posts: n/a

 07-29-2005
Bhanu

Anonymous 7843
Guest
Posts: n/a

 07-29-2005
In article <(E-Mail Removed). com>,
<(E-Mail Removed)> 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
Guest
Posts: n/a

 07-29-2005
"Bhan" <(E-Mail Removed)> writes:
> 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

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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
Guest
Posts: n/a

 07-29-2005
"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Bhan" <(E-Mail Removed)> writes:
>> 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
Guest
Posts: n/a

 07-29-2005
In article <(E-Mail Removed). com>,
(E-Mail Removed) 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
Guest
Posts: n/a

 07-30-2005
<(E-Mail Removed)> 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
Guest
Posts: n/a

 07-30-2005
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