timeit模块提供了一种简便的方法来为Python中的小块代码进行计时。
模块调用函数,stmp为要测试的函数,setup为测试环境,number为运行次数
timeit.timeit(stmt='pass', setup='pass' ,number=1000000)
在脚本文件中的简单用法:
# 要测试的函数
def fun(params): [x for x in range(params)]
import timeit default_number = 10000
# 测试环境 setup = """ from __main__ import fun params = 10 """ print(timeit.timeit("fun(params)",setup=setup,number=default_number))