Re: Optimizing a switch statement

From: Greg Comeau (comeau_at_panix.com)
Date: 03/04/04


Date: 3 Mar 2004 23:42:21 -0500

In article <4046A691.1050904@sbcglobal.net>,
Thomas Matthews <Thomas_MatthewsSpamBotsSuck@sbcglobal.net> wrote:
>My son is writing a program to move a character. He is
>using the numbers on the keypad to indicate the direction
>of movement:
> 7 8 9
> 4 5 6
> 1 2 3
>Each number has a direction except for '5'. So in
>his switch statement, he omits a case for '5':
>/* char direction; */
> switch (direction)
> {
> case '1':
> move_lower_left();
> break;
> case '2':
> move_down();
> break;
> case '3':
> move_lower_right();
> break;
> case '4':
> move_left();
> break;
> case '6':
> move_right();
> break;
> case '7':
> move_upper_left();
> break;
> case '8':
> move_up();
> break;
> case '9':
> move_upper_right();
> break;
> } /* end switch direction */
>
>The case selectors are not contiguous in the above
>example, since case '5' is omitted (on purpose).
>
>My question is: Would adding a case for '5' reduce
>the code space and execution time for this switch
>statement?

Maybe, maybe not.

>The logic behind this is that a contigous set of
>case selectors would allow the compiler to create
>a "jump table", where as the above example may
>force the compiler to generate an "if-elseif"
>ladder.

You don't know. And trying to second guess implementations
in this case seems to be programming at the wrong level.
Just to toss more wrenches in, there is also std::map's
and such as well as a good 'ol array of structs you can
build too.

-- 
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==>     http://www.comeaucomputing.com/tryitout
World Class Compilers:  Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?


Relevant Pages

  • Re: Optimizing a switch statement
    ... >his switch statement, he omits a case for '5': ... >The case selectors are not contiguous in the above ... World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. ... Comeau C/C++ with Dinkumware's Libraries... ...
    (comp.lang.cpp)
  • Re: Optimizing a switch statement
    ... >his switch statement, he omits a case for '5': ... >The case selectors are not contiguous in the above ... World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. ... Comeau C/C++ with Dinkumware's Libraries... ...
    (comp.lang.c)
  • Re: 65536 Switch Cases
    ... generator problems. ... Most people don't need 65K switch statements, ... World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. ... Comeau C/C++ with Dinkumware's Libraries... ...
    (comp.lang.cpp)
  • Re: C++ a little help please.
    ... >> int main ... >The switch statement with a default is a good idea, ... |was left in the input stream etc... ... Comeau C/C++ with Dinkumware's Libraries... ...
    (alt.comp.lang.learn.c-cpp)
  • Re: writing to a string
    ... If you mean file sorting then you'd be using ... >guranteed to work on any platfrom in existence then you are no longer just ... World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. ... Comeau C/C++ with Dinkumware's Libraries... ...
    (alt.comp.lang.learn.c-cpp)