switch statement optimizations

Discussion in 'C++' started by Rob Adelberg, Sep 18, 2003.

  1. Rob Adelberg

    Rob Adelberg Guest

    I have a single switch statement with about 30 case statements.

    I could break the single switch into 3 or 4 switchs, which logically
    would be more efficient.

    switch()
    {
    switch() // only 10 cases

    switch() // only 10 cases
    }
    But with compiler optimizations would it make any difference? Would
    one way be better than the other?

    I'm using MSVC 6
     
    Rob Adelberg, Sep 18, 2003
    #1
    1. Advertising

  2. On 18 Sep 2003 09:12:42 -0700, (Rob Adelberg) wrote:

    >I have a single switch statement with about 30 case statements.


    Perhaps you're stuck with someone else's code.



    >I could break the single switch into 3 or 4 switchs, which logically
    >would be more efficient.


    Nope, no logic in that.



    > switch()
    > {
    > switch() // only 10 cases
    >
    > switch() // only 10 cases
    > }
    >But with compiler optimizations would it make any difference? Would
    >one way be better than the other?


    The C++ standard has nothing to say about that.



    >I'm using MSVC 6


    Then ask in a Microsoft group (use news.microsoft.com if necessary),
    it's off-topic here (see the FAQ).
     
    Alf P. Steinbach, Sep 18, 2003
    #2
    1. Advertising

  3. Rob Adelberg wrote:
    > I have a single switch statement with about 30 case statements.
    >
    > I could break the single switch into 3 or 4 switchs, which logically
    > would be more efficient.


    Huh?

    > switch()
    > {
    > switch() // only 10 cases
    >
    > switch() // only 10 cases
    > }
    > But with compiler optimizations would it make any difference? Would
    > one way be better than the other?
    >
    > I'm using MSVC


    Switch is often a computed goto, especially if the cases are
    reasonably contiguous. (Ooh, deja vu. I must be paraphrasing
    someone, but I can't remember whom.) So with a single switch
    you get a single computation and a single goto. In any case,
    it's the compiler's job to choose the most efficient
    implementation.

    Regards,
    Buster.
     
    Buster Copley, Sep 18, 2003
    #3
  4. Buster Copley wrote:
    > Rob Adelberg wrote:
    >
    >> I have a single switch statement with about 30 case statements.
    >>
    >> I could break the single switch into 3 or 4 switchs, which logically
    >> would be more efficient.

    >
    >
    > Huh?
    >
    >> switch()
    >> {
    >> switch() // only 10 cases
    >>
    >> switch() // only 10 cases
    >> }
    >> But with compiler optimizations would it make any difference? Would
    >> one way be better than the other?
    >>
    >> I'm using MSVC

    >
    >
    > Switch is often a computed goto, especially if the cases are
    > reasonably contiguous.


    Switch _is_ a computed goto, obviously. Sorry. The person I was
    paraphrasing was saying that it's often implemented using a jump
    table.
     
    Buster Copley, Sep 18, 2003
    #4
  5. Rob Adelberg

    red floyd Guest

    Rob Adelberg wrote:

    > I have a single switch statement with about 30 case statements.
    >
    > I could break the single switch into 3 or 4 switchs, which logically
    > would be more efficient.
    >
    > switch()
    > {
    > switch() // only 10 cases
    >
    > switch() // only 10 cases
    > }
    > But with compiler optimizations would it make any difference? Would
    > one way be better than the other?
    >
    > I'm using MSVC 6


    Don't worry about efficiency. IMHO, the difference would be minimal. Go with what's
    more readable and maintainable. Remember, either you or somebody else will probably
    have to look at this a couple of years down the line, and if you make it easier to
    deal with then, you've got a win.
     
    red floyd, Sep 18, 2003
    #5
  6. Rob Adelberg

    Ron Natalie Guest

    "Rob Adelberg" <> wrote in message news:...
    > I have a single switch statement with about 30 case statements.
    >
    > I could break the single switch into 3 or 4 switchs, which logically
    > would be more efficient.


    That would probably not help. If the number of possible values versus
    the number of cases is manageable, the compiler just generates a jump
    table in most dcases.
     
    Ron Natalie, Sep 18, 2003
    #6
    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. niju

    Optimizations

    niju, Aug 21, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    397
    tom pester
    Aug 21, 2005
  2. =?Utf-8?B?Sm9obiBHcmFudA==?=

    Webpart optimizations

    =?Utf-8?B?Sm9obiBHcmFudA==?=, May 2, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    1,661
    Robert Bogue [MVP]
    May 26, 2006
  3. Replies:
    21
    Views:
    1,060
    Giannis Papadopoulos
    Aug 2, 2005
  4. bthumber
    Replies:
    5
    Views:
    421
    Alexey Smirnov
    Jan 29, 2009
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    110
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page