timeit.py 368 B

1234567891011121314
  1. from functools import wraps
  2. import time
  3. # 函数运行计数装饰器
  4. def timeit(func):
  5. @wraps(func)
  6. def wrapper(*args, **kwargs):
  7. start = time.perf_counter()
  8. result = func(*args, **kwargs)
  9. elapsed = time.perf_counter() - start
  10. print(f"函数 {func.__name__} 耗时: {elapsed:.4f}秒")
  11. return result
  12. return wrapper