两个选项:
>在您的定时代码中包含“打印”.丑,但嘿.
timer = Timer('print func()', 'from __main__ import func')
print timer.timeit(1)
>如果你所做的一切都是运行你的功能一次,然后省时间模块,并使用相同的方法直接使用代码:
import sys
import time
if sys.platform == "win32":
# On Windows, the best timer is time.clock()
default_timer = time.clock
else:
# On most other platforms the best timer is time.time()
default_timer = time.time
t0 = default_timer()
output = func()
t1 = default_timer()
print output, t1 - t0
如果要多次运行代码,并产生输出,为什么不在timeit函数之外运行代码?你已经不止一次地打电话了:
timer = Timer('func()', 'from __main__ import func')
print timer.timeit(100),
print func()