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

Discussion in 'C Programming' started by swaroophr@gmail.com, Jul 29, 2005.

  1. Guest

    Which of switch statement and if-else statement takes less time to
    execute?
    , Jul 29, 2005
    #1
    1. Advertising

  2. Emmanuel Delahaye, Jul 29, 2005
    #2
    1. Advertising

  3. Emmanuel Delahaye, Jul 29, 2005
    #3
  4. Bhan Guest

    I am trying the same.Please help me out
    Thanks for your reply
    Bhanu
    Bhan, Jul 29, 2005
    #4
  5. In article <>,
    <> 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++
    Anonymous 7843, Jul 29, 2005
    #5
  6. Re: Which of switch statement and if-else statement takes less timeto execute?

    "Bhan" <> 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) <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.
    Keith Thompson, Jul 29, 2005
    #6
  7. Mark Guest

    "Keith Thompson" <> wrote in message
    news:...
    > "Bhan" <> 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?
    :)
    Mark, Jul 29, 2005
    #7
  8. In article <>,
    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.
    Christian Bau, Jul 29, 2005
    #8
  9. Malcolm Guest

    <> 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.
    Malcolm, Jul 30, 2005
    #9
  10. Neo Guest

    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
    Neo, Jul 30, 2005
    #10
  11. Jack Klein Guest

    On 29 Jul 2005 06:45:40 -0700, 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/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jul 30, 2005
    #11
  12. Joe Wright Guest

    Re: Which of switch statement and if-else statement takes less timeto execute?

    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 ---
    Joe Wright, Jul 30, 2005
    #12
  13. Re: Which of switch statement and if-else statement takes less timeto execute?

    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
    Peter Pichler, Jul 31, 2005
    #13
  14. John Bode Guest

    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;

    ...
    }
    John Bode, Jul 31, 2005
    #14
  15. Bhan Guest

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


    Mark wrote:
    > "Keith Thompson" <> wrote in message
    > news:...
    > > "Bhan" <> 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?
    > :)
    Bhan, Aug 1, 2005
    #15
  16. CBFalconer Guest

    Re: Which of switch statement and if-else statement takes less time toexecute?

    *** rude top-posting fixed ***
    Bhan wrote:
    > Mark wrote:
    >> "Keith Thompson" <> wrote in message
    >>> "Bhan" <> 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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Aug 1, 2005
    #16
  17. In article <>,
    Bhan <> wrote:
    >
    >If you know the answer tell me.


    The answer is 42.

    >Else keep quite.


    Quite what?
    Kenny McCormack, Aug 1, 2005
    #17
  18. Re: Which of switch statement and if-else statement takes less time toexecute?

    CBFalconer wrote:
    >
    > *** rude top-posting fixed ***
    > Bhan wrote:
    > > Mark wrote:
    > >> "Keith Thompson" <> wrote in message
    > >>> "Bhan" <> 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 |
    +----------------------------------------------------------------+
    Charles Richmond, Aug 1, 2005
    #18
  19. 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
    Lawrence Kirby, Aug 1, 2005
    #19
  20. Default User Guest

    Bhan wrote:

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



    *paaaalonk*



    Brian
    Default User, Aug 1, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nicholas
    Replies:
    7
    Views:
    594
    John.Net
    Aug 3, 2005
  2. Replies:
    12
    Views:
    1,458
    Christian Bau
    May 17, 2005
  3. jiajia wu
    Replies:
    0
    Views:
    348
    jiajia wu
    Oct 1, 2009
  4. 6668
    Replies:
    0
    Views:
    142
  5. lllll
    Replies:
    0
    Views:
    123
    lllll
    Jun 8, 2009
Loading...

Share This Page