无参装饰器为被装饰函数添加统计时间的功能

#需求 定义无参装饰器为被装饰函数添加统计时间的功能
 1 import time  #导入时间模块
 2 
 3 def timer(func):
 4     def wapper():
 5         start = time.time()
 6         func()
 7         stop = time.time()
 8         index_spend_time = stop - start
 9         print(index_spend_time)
10     return wapper
11 @timer
12 def index():
13     time.sleep(2)
14     print("welcone beijing")
15 index()

 

posted @ 2017-04-12 20:00  shy车队破风手  阅读(224)  评论(0编辑  收藏  举报