The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

Mark Lawrence breamoreboy at yahoo.co.uk
Mon Mar 21 20:42:22 EDT 2016


On 22/03/2016 00:18, BartC wrote:
> On 21/03/2016 23:50, Terry Reedy wrote:
>> On 3/21/2016 8:43 AM, BartC wrote:
>>
>>> This tests highlights the benefits of an O(1) switch statement.
>>
>> So why are you not taking advantage of Python's O(1) dict lookup?
>>
> I've already reported using a list lookup which is also O(1), and it
> didn't really help. I doubt a dict lookup is going to be faster (but
> Python has surprised me before).

Please explain how you managed to make the list lookup O(1).

>
> One problem is how to attach the handling code to the entry in the list
> or as the value in the dict. With the list, I stored a function reference.

It has repeatedly been stated that a dict is used as a switch 
replacement, so there is no problem attaching a handler to a dict.

>
> I suspect that a switch implemented Python-style wouldn't be
> dramatically faster either. But the handling code can be expressed in-line.
>

There are umpteen recipes of all shapes and sizes that implement 
switch/case statements, even pattern matching, online.  Why don't you 
time some of them, as relying on gut instinct is almost always wrong in 
Python.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list