list(filter(lamba: c: c>127,map(ord,symbols)))
listcomp : 0.044 0.043 0.040 0.037 0.035 listcomp + func : 0.056 0.056 0.056 0.055 0.054 filter + lambda : 0.048 0.049 0.052 0.068 0.035 filter + func : 0.027 0.030 0.041 0.042 0.043
import timeit
TIMES = 10000
SETUP = """ symbols = '$¢£¥€¤' def non_ascii(c): return c > 127 """
def clock(label, cmd): res = timeit.repeat(cmd, setup=SETUP, number=TIMES) print(label, *('{:.3f}'.format(x) for x in res))
clock('listcomp :', '[ord(s) for s in symbols if ord(s) > 127]') clock('listcomp + func :', '[ord(s) for s in symbols if non_ascii(ord(s))]') clock('filter + lambda :', 'list(filter(lambda c: c > 127, map(ord, symbols)))') clock('filter + func :', 'list(filter(non_ascii, map(ord, symbols)))')
image.png
image.png
image.png
from collections import namedtuple City=namedtuple('City','name country population coordination') tokyo=City('Tokyo''JP',36.93,(35.5,129))
image.png
image.png
[1:3,2:2]
list.sort不会新建一个列表 sorted 会新建一个列表
image.png
bisect
from array import array floats=array('d',(random() for i in range(1000000000)))
image.png
image.png
image.png
image.png
image.png
image.png