performance.py 465 B

12345678910111213141516
  1. import functools
  2. import time
  3. def timer(func):
  4. """Print the runtime of the decorated function"""
  5. @functools.wraps(func)
  6. def wrapper_timer(*args, **kwargs):
  7. start_time = time.perf_counter() # 1
  8. value = func(*args, **kwargs)
  9. end_time = time.perf_counter() # 2
  10. run_time = end_time - start_time # 3
  11. print(f"Finished {func.__name__!r} in {run_time:.4f} secs")
  12. return value
  13. return wrapper_timer