之前做性能度量时,一直用time模块的time方法,其实python已经有相关的模块可用。在此简单介绍一下 timeit 模块,
#!/usr/bin/env python
from timeit import Timer
def foo():
a = ['1', '2', '3', '4']
map(lambda x: "%s-xxx" % x, a)
t = Timer('foo', 'from __main__ import foo')
print t.repeat(repeat=3, number=100000000)
上面的意思就是重复执行3次foo函数, 每次执行过程中,函数被运行100000000次。
更多关于timeit的细节,请阅读python官方文档。
https://docs.python.org/2/library/timeit.html?highlight=timeit#timeit