1:
import time
from functools import wraps
def timethis(func):
'''
Decorator that reports the execution time.
'''
@wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(params)
print(func.__name__, end-start)
return result
return wrapper
@timethis()
def countdown(n):
'''
Counts down
'''
while n > 0:
n -= 1
countdown(10000000)
print(countdown.__name__)
包装器以@开头, 此时调用countdown(10000) 就相当于调用wrapper,
@wraps(func) 作用: 让countdown()的属性不改变(例如 __name__ 等); 效果:
加上: 最后输出countdown 不加: 最后输出wrapper