Connecting Tech Pros Worldwide Forums | Help | Site Map

switch statement optimizations

Rob Adelberg
Guest
 
Posts: n/a
#1: Jul 19 '05
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

Alf P. Steinbach
Guest
 
Posts: n/a
#2: Jul 19 '05

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).

Buster Copley
Guest
 
Posts: n/a
#3: Jul 19 '05

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.

Buster Copley
Guest
 
Posts: n/a
#4: Jul 19 '05

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.

red floyd
Guest
 
Posts: n/a
#5: Jul 19 '05

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.

Ron Natalie
Guest
 
Posts: n/a
#6: Jul 19 '05

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.


Closed Thread


Similar C / C++ bytes