Re: Optimizing a switch statement

From: David Rubin (fullname_at_warpmail.net)
Date: 03/04/04


Date: Thu, 04 Mar 2004 13:58:54 GMT

Christian Bau wrote:
> In article <slrnc4dp1l.4qe.vek@station02.ohout.pharmapartners.nl>,
> Villy Kruse <vek@station02.ohout.pharmapartners.nl> wrote:
>
>
>>On Thu, 04 Mar 2004 04:11:13 GMT,
>> Andrew Koenig <ark@acm.org> wrote:
>>
>>
>>
>>>>A better solution is to use a lookup table of function pointers:
>>>
>>>Why is it better?
>>>
>>>
>>
>>
>>A half decent compiler will most likely implement the case statement using
>>a look up table anyway, especial when the case values is in a very limited
>>range as 10 posibilities. Thus, using a table of function pointers may
>>actualy be slower.
>
>
> It is also much more work to write, you have to declare the array,
> initialise it, get that right, you have to do the range checking
> yourself, the speed doesn't matter anyway for this code. Now imagine you
> have a switch that involves letters 'A', 'B', 'C' and so on. The
> function pointer array code now gets really interesting... In the case
> statement, you just add more cases.

I think the lookup table solution is better because the control is
expressed more succinctly. I don't think it is more or less difficult to
add new options to the array than it is to add them to the switch
statement. I take your point that changing from numbers to letters
weakens the argument for function tables if not just for the fact that
the table may waste a bit of memory. Although OP asked about
'optimization,' it is well known that this kind of micro-optimization is
usually not worth the effort compared to code maintainability.

"Better," is always a subjective judgement. In my opinion, a function
table is a better solution than a switch statement, which I view as
unwieldy. Switch statements /are/ good, especially when you want to vary
the response to the variable being switched beyond calling one of a set
of homogeneous functions.

I'm not sure I understand why a lookup table generated by the compiler
for a switch statement would not be equivalent to a handmade lookup
table using function pointers. Villy Kruse suggests that the second
method might be slower than the first. Maybe the point is that
internally generated 'goto' statements are faster than accessing array
elements.

/david

-- 
"As a scientist, Throckmorton knew that if he were ever to break wind in
the echo chamber, he would never hear the end of it."


Relevant Pages

  • Re: Hundreds of cases in a switch, optimization?
    ... i meant that the switch statement _didnt_ use 200 comparisions. ... such jumps do make sense for ... > of function pointers? ... in assembly it probably resembles an array of "jump" pointers... ...
    (comp.lang.c)
  • Re: [PATCH 0/5] partitions: Changes to fs/partitions for readability and efficiency
    ... I'm also unconvinced that an array of function pointers is any harder to ... if (err) ... More majordomo info at http://vger.kernel.org/majordomo-info.html ...
    (Linux-Kernel)
  • Re: [OT] Switch question
    ... > near the top of the switch statement or should I consider making a lookup ... > table of function pointers instead (but the function call overhead might ... Sean ...
    (comp.lang.c)
  • Re: Is it possible to make void * safer?
    ... that's one big hit when simulating templates over using C++ templates. ... Unless you run it through a C++ compiler, ... That's why I added the function pointers, ... array_push(array, data);} ...
    (comp.lang.c)
  • Re: debugging - help
    ... i am executing on Armulator using AXD debugger. ... What i have is an array of function pointers which get periodically ... I parse this array each time, calling the functions ...
    (comp.lang.c)