python 写一个计算执行时间的装饰器

发布时间:2017-09-07 14:59:42
python 写一个计算执行时间的装饰器

首先,计算函数的执行时间:

import time start = time.clock() for i in range(1000000): pass end = time.clock() print cost time = %f s % (end-start) 结果:
>>> cost time = 0.092749 s >>>

然后:

import time def warps(t): def deco(func): def _deco(*args,**kwargs): start = time.clock() func(*args,**kwargs) end = time.clock() if end - start > t: print 'bad' else: print 'goods' return _deco return deco @warps(10) def myfunc(*args,**kwargs): for i in range(100000): pass myfunc()

结果:

>>> goods >>>

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉网络推广 https://www.feimao666.com