赞
踩
lt = range(10000)
te = tuple(lt)
st = set(lt)
%%timeit
for i in lt:
a = i
结果:399 µs ± 11.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%%timeit
for i in te:
a = i
结果:231 µs ± 6.34 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%%timeit
for i in st:
a = i
结果:274 µs ± 7.47 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
遍历数量较大(500)的情况下:
速度:元组(tuple) > 集合(set) > 列表(list)
遍历数量较小(100)的情况下:
速度:元组(tuple) > 列表(list) > 集合(set)
tuple是不可改变的,不仅遍历速度更快、而且内存更小
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。