装饰器应用(计算函数的执行时间)

泄露秘密 提交于 2019-12-03 06:50:12
#_author:Administrator#date:2019/11/2#优化#先把展示时间的函数加载进内存import timedef show_time(f):    def inner():        start=time.time()        f()        end=time.time()        print('spend time:%s'%(end-start))    return inner@show_time #此处的@show_time相当于function1=show_time(function1)def function1():    print('excute function<<')    time.sleep(2)function1()print('---------------------')@show_timedef function2():    print('excute function2<<')    time.sleep(2)function2()    #function1=show_time(function1) 相当于返回inner# 执行结果:# excute function<<# spend time:2.0004312992095947# ---------------------# excute function2<<# spend time:2.0000228881835938
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!