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

BartC bc at freeuk.com
Tue Mar 15 14:28:10 EDT 2016


On 15/03/2016 14:58, BartC wrote:
> On 15/03/2016 11:52, BartC wrote:
>> On 15/03/2016 01:55, Steven D'Aprano wrote:
>
>>> switch obj:
>>>      case "Hello", None: ...
>>>      case [1, 2, 3]: ...
>>>      case 23.01, 15+2j, Fraction(10, 11): ...
>>>      case 100**100, {}: ...
>>>
>>>
>>> and more. This is not negotiable: having a switch statement limited to
>>> small
>>> ints is simply not an option.
>>
>> Not a problem: http://pastebin.com/qdQintSZ
>
> The solution I posted tested the values one after another. I was
> interested in whether it was that much faster than if-elif in CPython 3,

> data=["Hello",None,[1,2,3],23.01,15+2j,Fraction(10,11),100**100,{},12345]
....

 > ... so something is slowing down the Python in this test

The culprit here was Fraction. Whatever Fraction does, it's slow! And 
when removed from the test expressions, it was still in the data list.

Replacing Fraction with a dummy class gave a more reasonable timing of 
1.5 seconds (compared with 0.17s for the external language using 
'general' switch, and 0.28s when it also uses an if-else chain).

-- 
Bartc



More information about the Python-list mailing list