Re: Optimizing a switch statement
From: David Rubin (fullname_at_warpmail.net)
Date: 03/04/04
- Next message: Thomas Matthews: "Re: Optimizing a switch statement"
- Previous message: Richard Bos: "Re: Increasing efficiency in C"
- In reply to: Christian Bau: "Re: Optimizing a switch statement"
- Next in thread: Dietmar Kuehl: "Re: Optimizing a switch statement"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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."
- Next message: Thomas Matthews: "Re: Optimizing a switch statement"
- Previous message: Richard Bos: "Re: Increasing efficiency in C"
- In reply to: Christian Bau: "Re: Optimizing a switch statement"
- Next in thread: Dietmar Kuehl: "Re: Optimizing a switch statement"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|