Re: Optimizing a switch statement

From: Derk Gwen (derkgwen_at_HotPOP.com)
Date: 03/04/04


Date: Thu, 04 Mar 2004 17:53:41 -0000

Julie <julie@aol.com> wrote:
# David Rubin wrote:
# >
# > Thomas Matthews wrote:
# > > 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; */

# > > My question is: Would adding a case for '5' reduce
# > > the code space and execution time for this switch
# > > statement?

It's a quality of implementation issue that has no general answer. The
implementation might recognise the missing 5 as a junmp to esac and
insert that in a jump table. It might be serially testing each case no
matter what. It might be using a binary tree or hash table. No way to know
in general.

# > A better solution is to use a lookup table of function pointers:

# That's what I was thinking. But you can even optimize away the if() by
# creating a null function for the '5', something like:

If you want fast, you don't want function pointers. The're very hard to
optimise.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
I'm not even supposed to be here today.


Relevant Pages

  • Re: Optimizing a switch statement
    ... >> My son is writing a program to move a character. ... > A better solution is to use a lookup table of function pointers: ...
    (comp.lang.cpp)
  • Re: Optimizing a switch statement
    ... >> My son is writing a program to move a character. ... > A better solution is to use a lookup table of function pointers: ...
    (comp.lang.c)
  • Re: Optimizing a switch statement
    ... >> My son is writing a program to move a character. ... > A better solution is to use a lookup table of function pointers: ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Optimizing a switch statement
    ... #>> My son is writing a program to move a character. ... you don't want function pointers. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Optimizing a switch statement
    ... #>> My son is writing a program to move a character. ... you don't want function pointers. ...
    (comp.lang.cpp)