17 lines
566 B
Python
17 lines
566 B
Python
import time
|
|
|
|
def time_cost(tag=None):
|
|
"""
|
|
装饰器,自定义函数耗时打印信息
|
|
:param tag: 可选,自定义标志(说明当前执行的函数)
|
|
"""
|
|
def decorator(func):
|
|
def wrapper(*args, **kwargs):
|
|
start_time = time.time()
|
|
result = func(*args, **kwargs)
|
|
end_time = time.time()
|
|
label = tag if tag is not None else func.__name__
|
|
print(f"耗时统计[{label}]: {(end_time - start_time)*1000:.2f} 毫秒")
|
|
return result
|
|
return wrapper
|
|
return decorator |