The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)
Paul Rubin
no.email at nospam.invalid
Sun Mar 13 19:25:43 EDT 2016
BartC <bc at freeuk.com> writes:
> def case(*args):
> return any((arg == switch.value for arg in args))
def case(values):
return switch.value in values
> I used it in my benchmark to replace the if-else chain checking three
> lots of ranges:
>
> switch(c)
> if case(ord("A"),ord("B"),ord("C"),ord("D"),ord("E"),ord("F"),
> ord("G"),ord("H"),ord("I"),ord("J"),ord("K"),ord("L"),
> ord("M"),ord("N"),ord("O"),ord("P"),ord("Q"),ord("R"),
> ord("S"),ord("T"),ord("U"),ord("V"),ord("W"),ord("X"),
> ord("Y"),ord("Z")):
> upper+=1
Use a set instead of a big tuple:
uppers = { ord("A"), ord("B"), ... ord("Z") }
...
if case(uppers):
upper += 1
More information about the Python-list
mailing list