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,
so I tried the test similar to that below, except that k, c and f were
literal values in the loop to start with.
Initial timings were 5 seconds for mine, 12.5 seconds for Python. That's
in line with what I expected when dealing with more complex objects.
However, I then took the 100**100 outside the loop in my version (as
that expression was not reduced to a constant). The timing then reduced
to 170ms (I have a slow big num library).
But doing the same with Python made no difference. Taking Complex and
Fraction outside reduced the timing to 8 seconds, still 50 times slower.
(My language doesn't understand Complex and Fraction, so testing against
those is a quick process. Taking those out completely as well as
100**100 made a difference, but there was the same discrepancy)
I know my language isn't that fast, so something is slowing down the
Python in this test (and I don't /think/ I've left a zero out in my
version!)
So maybe it makes the case for a proper Switch in Python stronger where
such comparisons could be streamlined.
from fractions import Fraction
def test():
data=["Hello",None,[1,2,3],23.01,15+2j,Fraction(10,11),100**100,{},12345]
# print (data)
k=100**100
c=15+2j
f=Fraction(10,11)
for n in range(100000):
for obj in data:
if obj=="hello" or obj==None:
pass
elif obj==[1,2,3]:
pass
elif obj==23.01 or obj==c or obj==f:
pass
elif obj==k or obj=={}:
pass
test()
--
Bartc
--
https://mail.python.org/mailman/listinfo/python-list