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
>>>