Python有自己的timeit模块,但是用惯了matlab的tic/toc,模仿了一个,需要用到globals()函数.
[Python]代码
import time
def tic():
globals()['tt'] = time.clock()
def toc():
print '\nElapsed time: %.8f seconds\n' % (time.clock()-globals()['tt'])
使用示例
from mytictoc import tic, toc
tic()
for i in range(100000):
pass
toc()
运行结果
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Elapsed time: 0.01879716 seconds
>>>