1. 简介

27.5. timeit — Measure execution time of small code snippets

Source code: Lib/timeit.py

timeit模块可以用来测试一段代码的执行耗时,如一个变量赋值语句的执行时间,一个函数的运行时间。

timeit是标准模块,无需安装。

import timeit

1.1. python interface

模块定义了三个方法和一个公共类。

1.1.1. timeit.timeit(stmt='pass', setup='pass', timer=, number=1000000, globals=None)

Create a Timer instance with the given statement, setup code and timer function and run its timeit() method with number executions. The optional globals argument specifies a namespace in which to execute the code.

参数说明:

  • stmt:测试代码,一般用函数(可以用字符串表达式)。
  • setup:传入stmt的运行环境,如参数,变量,模块。
  • timer:
  • number:执行次数,默认1000000
  • globals:

1.1.2. timeit.repeat(stmt='pass', setup='pass', timer=, repeat=3, number=1000000, globals=None)

Create a Timer instance with the given statement, setup code and timer function and run its repeat() method with the given repeat count and number executions. The optional globals argument specifies a namespace in which to execute the code.

参数说明:

  • repeat:重复次数,每次的结果构成列表返回,默认3次。

1.1.3. timeit.default_timer()

The default timer, which is always time.perf_counter().

Changed in version 3.3: time.perf_counter() is now the default timer.

1.1.4. class timeit.Timer(stmt='pass', setup='pass', timer=, globals=None)

Class for timing execution speed of small code snippets.

2. 案例

2.1. timeit() /repeat()

注意事项:

  1. 方法应在setup参数中导入;
  2. 变量需在setup参数中导入,且变量应为全局变量或直接传入;
  3. 测试次数number默认为10**6,注意修改,特别是单次执行时间较长时。
import timeit

def _test_timeit():
global a # timeit
a = [1,2,3,4]
t = timeit.timeit(stmt='_test_2(a)',
setup='from __main__ import _test_2, a',
number=100000)
print(t) # repeat
a = [1,2,3,4]
t = timeit.repeat(stmt='_test_2(a)',
setup='from __main__ import _test_2, a',
number=1000000,
repeat=5 )
print(t) def _test_2(s=None, *ar):
#s.insert(0, 45)
s.append(55) if __name__ == '__main__':
a = None
_test_timeit()
pass

输出:

0.02622983706364665
[0.28630844773090425, 0.2964419925588122, 0.23573263042489412, 0.2578145301438086, 0.22425034115163478]

说明:

repeat()与timeit()的区别在于它会重复测试多次,返回单次执行时间所组成的列表。

2.2. timer()

timer()实质是timeit()和repeat()的底层实现;

def timeit(stmt="pass", setup="pass", timer=default_timer,
number=default_number, globals=None):
"""Convenience function to create Timer object and call timeit method."""
return Timer(stmt, setup, timer, globals).timeit(number) def repeat(stmt="pass", setup="pass", timer=default_timer,
repeat=default_repeat, number=default_number, globals=None):
"""Convenience function to create Timer object and call repeat method."""
return Timer(stmt, setup, timer, globals).repeat(repeat, number)

也可以使用Timer()初始化然后调用其方法timeit和repeat。

最新文章

  1. Android之仿微信Tab滑动
  2. 小结一下前段时间做的rpgdemo
  3. git 找回丢失的commit
  4. .net社区
  5. flume+hadoop
  6. Android常见开发思路
  7. 将html中的br换行符转换为文本输入中的换行符(转)
  8. css3动画 9步
  9. 【转】国外程序员整理的Java资源大全
  10. java二维码生成
  11. POJ 1681 Painter's Problem [高斯消元XOR]
  12. 获得随机N位数不重复数字
  13. get_time
  14. HDU 4320 Arcane Numbers 1(质因子包含)
  15. WorldWind源码剖析系列:配置载入器类ConfigurationLoader
  16. Beta版本讨论
  17. nyoj 单调递增最长子序列
  18. array_column()提取二维数组中某个值
  19. thinkphp3.2.3代码审计
  20. Spring学习笔记(四)-- Spring事务全面分析

热门文章

  1. 曼孚科技:数据标注,AI背后的百亿市场
  2. eclipse调字体大小
  3. 在myEclipse中根据图表自动生成Hibernate文件
  4. ueditor使用本地保存,自动恢复上次编辑的内容
  5. python多项式拟合:np.polyfit 和 np.polyld
  6. 详解C/C++中的的:#pragma pack(push) 、#pragma pack(pop) 和#pragma pack()
  7. centos7下编译安装redis5.05
  8. importing-cleaning-data-in-r-case-studies
  9. centos7安装谷歌浏览器
  10. ubuntu 安装谷歌