import time
# 定义一个修饰器,用来计算函数的执行时间
def show_time(func):
def inner():
start_time = time.time()
func()
end_time = time.time()
print('spend %s' % (end_time - start_time))
return inner
# @show_time将修饰器注入到函数当中,类似foo = show_time(foo)
@show_time
def foo():
print('我需要一个修饰器')
time.sleep(2)
# foo = show_time(foo)
foo()
网友评论