Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > switch statement optimizations

Reply
Thread Tools

switch statement optimizations

 
 
Rob Adelberg
Guest
Posts: n/a
 
      09-18-2003
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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      09-18-2003
On 18 Sep 2003 09:12:42 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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).

 
Reply With Quote
 
 
 
 
Buster Copley
Guest
Posts: n/a
 
      09-18-2003
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.

 
Reply With Quote
 
Buster Copley
Guest
Posts: n/a
 
      09-18-2003
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.

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      09-18-2003
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.

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      09-18-2003

"Rob Adelberg" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) m...
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
My Switch statement get stuck on the last statement bthumber ASP .Net 5 01-29-2009 07:56 PM
Webpart optimizations =?Utf-8?B?Sm9obiBHcmFudA==?= ASP .Net 3 05-25-2006 11:12 PM
Optimizations niju ASP .Net 1 08-21-2005 04:57 PM
Which of switch statement and if-else statement takes less time to execute? swaroophr@gmail.com C Programming 21 08-02-2005 09:24 AM
An Evolutionary Analysis of GNU C Optimizations Scott Robert Ladd C++ 2 11-17-2003 11:20 PM



Advertisments