Connecting Tech Pros Worldwide Help | Site Map

switch statement optimizations

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 05:57 PM
Rob Adelberg
Guest
 
Posts: n/a
Default switch statement optimizations

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

  #2  
Old July 19th, 2005, 05:57 PM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: switch statement optimizations

On 18 Sep 2003 09:12:42 -0700, robert.adelberg@gd-decisionsystems.com (Rob Adelberg) wrote:
[color=blue]
>I have a single switch statement with about 30 case statements.[/color]

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


[color=blue]
>I could break the single switch into 3 or 4 switchs, which logically
>would be more efficient.[/color]

Nope, no logic in that.


[color=blue]
> 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?[/color]

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


[color=blue]
>I'm using MSVC 6[/color]

Then ask in a Microsoft group (use news.microsoft.com if necessary),
it's off-topic here (see the FAQ).

  #3  
Old July 19th, 2005, 05:57 PM
Buster Copley
Guest
 
Posts: n/a
Default Re: switch statement optimizations

Rob Adelberg wrote:[color=blue]
> 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.[/color]

Huh?
[color=blue]
> 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[/color]

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.

  #4  
Old July 19th, 2005, 05:57 PM
Buster Copley
Guest
 
Posts: n/a
Default Re: switch statement optimizations

Buster Copley wrote:[color=blue]
> Rob Adelberg wrote:
>[color=green]
>> 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.[/color]
>
>
> Huh?
>[color=green]
>> 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[/color]
>
>
> Switch is often a computed goto, especially if the cases are
> reasonably contiguous.[/color]

Switch _is_ a computed goto, obviously. Sorry. The person I was
paraphrasing was saying that it's often implemented using a jump
table.

  #5  
Old July 19th, 2005, 05:57 PM
red floyd
Guest
 
Posts: n/a
Default Re: switch statement optimizations

Rob Adelberg wrote:
[color=blue]
> 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[/color]

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.

  #6  
Old July 19th, 2005, 05:57 PM
Ron Natalie
Guest
 
Posts: n/a
Default Re: switch statement optimizations


"Rob Adelberg" <robert.adelberg@gd-decisionsystems.com> wrote in message news:f34aef3d.0309180812.6400573@posting.google.co m...[color=blue]
> 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.[/color]

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.


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.