Welcome 微信登录

首页 / 操作系统 / Linux / Python模仿matlab的tic/toc计时

python有自己的timeit模块,但是用惯了matlab的tic/toc,模仿了一个,需要用到globals()函数.[Python]代码import timedef tic():
    globals()["tt"] = time.clock()def toc():
    print " Elapsed time: %.8f seconds " % (time.clock()-globals()["tt"])使用示例from mytictoc import tic, toctic()
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>>>