Can global variable be passed into Python function?
Marko Rauhamaa
marko at pacujo.net
Sun Mar 2 10:26:46 EST 2014
Marko Rauhamaa <marko at pacujo.net>:
> Roy Smith <roy at panix.com>:
>
>> Python already has a switch statement. It's just spelled funny...
>>
>> [...]
>>
>> try:
>> raise value
>> except Case1:
>> print "did case 1"
>> except (Case2, Case3):
>> print "did either case 2 or 3"
>> else:
>> print "did default"
>
> Not bad! Definitely worth considering.
I wrote a simple test that switched between 26 "enums" using three
techniques and measured execution times (2,600,000 switching
operations). I also measured the time with no switching and subtracted
that time from the test times.
Results of the competition (performed with python3.2.3):
1. DICT DISPATCH TABLE (0.2 µs/switch)
2. IF-ELSE CHAIN (850% slower than DICT DISPATCH TABLE)
3. TRY-EXCEPT (3500% slower than DICT DISPATCH TABLE)
Marko
More information about the Python-list
mailing list