歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python模仿matlab的tic/toc計時

Python模仿matlab的tic/toc計時

日期:2017/3/1 10:41:37   编辑:Linux編程

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

>>>

Copyright © Linux教程網 All Rights Reserved