timeit测试函数执行时间
原创
©著作权归作者所有:来自51CTO博客作者wx62aaee4be59a4的原创作品,请联系作者获取转载授权,否则将追究法律责任
def list_append():
l = []
for i in range(5000):
l.append(i)
def list_insert():
l = []
for i in range(5000):
l.insert(0, i)
import timeit
# 默认执行1万次
append_spent = timeit.timeit(
setup='from __main__ import list_append',
stmt='list_append()',
number=10000,
)
print('list_append:', append_spent)
insert_spent = timeit.timeit(
setup='from __main__ import list_insert',
stmt='list_insert()',
number=10000,
)
print('list_insert:', insert_spent)
-------------------------------------------
个性签名:代码过万,键盘敲烂!!!