Re: Optimizing a switch statement
Thomas Matthews <Thomas_Matthew sSpamBotsSuck@s bcglobal.net> wrote in message news:<4046A691. 1050904@sbcglob al.net>...
hi ,
if i am not mistaken adding a case for 5 wont reduce the code length,
instead it wld become more. also the execution will depend upon the
fact that the jmp addresses are within the same segment or not,ie if
the jmp addr is in another segment it will take more time for the
jmp.also the case values 1,2,... need not be in the ascending order,
thay can be random as they simply pt to a jmp addr and the switch
statemnt will simply check the directn variable value and jmp to the
corr addr.
the order is of significance.
regarding the function ptr thing u can initialize a func ptr like int
(*p[..])(); and then the statement (*p[direction])(); wld do the job
for u.
ofcourse the elements of p[] shld be intialized.
this will save a lot of code space for u also the execution will be
faster.
also on an avg the ifelse statment is also slower then the switch
statment and the ur fucn will not be executed as an ifelse stmnt,
where it continues the search until a match is found.
thats wht i think is the situation, i might be mistaken aswell.
hope it helps.
mehul[color=blue]
> Hi,
>
> 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_righ t();
> 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_righ t();
> 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?
>
> 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.
>
> {I cross posted to the three newsgroups because
> this issue deals with a switch statement which
> exists in both the C and C++ languages. This
> may also be of interest to newbies, like my son.}
>
> --
> Thomas Matthews
>
> C++ newsgroup welcome message:
> http://www.slack.net/~shiva/welcome.txt
> C++ Faq: http://www.parashift.com/c++-faq-lite
> C Faq: http://www.eskimo.com/~scs/c-faq/top.html
> alt.comp.lang.l earn.c-c++ faq:
> http://www.raos.demon.uk/acllc-c++/faq.html
> Other sites:
> http://www.josuttis.com -- C++ STL Library book
> http://www.sgi.com/tech/stl -- Standard Template Library[/color]
Thomas Matthews <Thomas_Matthew sSpamBotsSuck@s bcglobal.net> wrote in message news:<4046A691. 1050904@sbcglob al.net>...
hi ,
if i am not mistaken adding a case for 5 wont reduce the code length,
instead it wld become more. also the execution will depend upon the
fact that the jmp addresses are within the same segment or not,ie if
the jmp addr is in another segment it will take more time for the
jmp.also the case values 1,2,... need not be in the ascending order,
thay can be random as they simply pt to a jmp addr and the switch
statemnt will simply check the directn variable value and jmp to the
corr addr.
the order is of significance.
regarding the function ptr thing u can initialize a func ptr like int
(*p[..])(); and then the statement (*p[direction])(); wld do the job
for u.
ofcourse the elements of p[] shld be intialized.
this will save a lot of code space for u also the execution will be
faster.
also on an avg the ifelse statment is also slower then the switch
statment and the ur fucn will not be executed as an ifelse stmnt,
where it continues the search until a match is found.
thats wht i think is the situation, i might be mistaken aswell.
hope it helps.
mehul[color=blue]
> Hi,
>
> 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_righ t();
> 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_righ t();
> 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?
>
> 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.
>
> {I cross posted to the three newsgroups because
> this issue deals with a switch statement which
> exists in both the C and C++ languages. This
> may also be of interest to newbies, like my son.}
>
> --
> Thomas Matthews
>
> C++ newsgroup welcome message:
> http://www.slack.net/~shiva/welcome.txt
> C++ Faq: http://www.parashift.com/c++-faq-lite
> C Faq: http://www.eskimo.com/~scs/c-faq/top.html
> alt.comp.lang.l earn.c-c++ faq:
> http://www.raos.demon.uk/acllc-c++/faq.html
> Other sites:
> http://www.josuttis.com -- C++ STL Library book
> http://www.sgi.com/tech/stl -- Standard Template Library[/color]
Comment