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
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to