In [1]:
import timeit
In [7]:
TIMES = 100000
SETUP = """
symbols = '$¢£¥€¤'
def non_ascii(c):
return c > 127
"""
In [8]:
def clock(label, cmd):
res = timeit.repeat(cmd, setup=SETUP, number=TIMES)
print(label, *(f'{x:.2f}' for x in res))
In [9]:
clock("listcomp\t:", '[ord(s) for s in symbols if ord(s) > 127]')
clock("listcomp + func\t:", '[ord(s) for s in symbols if non_ascii(ord(s))]')
clock("filter + lambda\t:", 'list(filter(lambda c: c > 127, map(ord, symbols)))')
clock("filter + func\t:", 'list(filter(non_ascii, map(ord, symbols)))')
In [ ]: