[Cython] Offtopic (was: Re: [Pyrex] switch statement)
Robert Bradshaw
robertwb at math.washington.edu
Fri Mar 14 10:02:01 CET 2008
On Mar 14, 2008, at 1:47 AM, Dag Sverre Seljebotn wrote:
>> Dag Sverre wrote a similar writeup: http://wiki.cython.org/
>> enhancements/switch . I think it's a good idea. For small lists
>> the savings will be minimal, but for larger ones there can be
>> significant gains.
>>
> I often question switch statements though, I'm actually quite happy
> that Guido didn't make them part of Python. Often something like
> this is more appropriate:
>
> handlers = {
> 500: http_ok,
> 404: http_not_found
> }
> handlers[retcode](request, response)
Yes, this is certainly better form. Of course, if request/response
were code blocks, one would have to do something a bit more clever
(e.g. make a dictionary of functions and then call one).
> Of course, since we are talking performance of the GCC compiler
> here, I suppose it's one of the cases where performance for a O(1)
> operation do count, and then it's a different matter. All I'm
> saying is that most code I see using switch statements would often
> be better off without it.
:)
Here's the latest use case that I wanted:
http://hg.sagemath.org/sage-main/file/ff0875e07b4e/sage/ext/
fast_eval.pyx (starting at line 294--ignore that last case, this is
because the function gets called tens of thousands of times in a row,
and I didn't want an uncommon case to make everything slower). An if-
elif translation would work great here.
You can see that I used the dictionary paradigm in that same file as
well.
- Robert
More information about the Cython-dev
mailing list