switch statement optimizations

R

Rob Adelberg

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
 
A

Alf P. Steinbach

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

Buster Copley

Rob said:
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.
 
B

Buster Copley

Buster said:
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.
 
R

red floyd

Rob said:
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.
 
R

Ron Natalie

Rob Adelberg said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top