Advertisement
repsac

Performance Test: String formatting for logging

Aug 3rd, 2024
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | Software | 0 0
  1. import time
  2. import logging
  3.  
  4. logging.basicConfig(level=logging.INFO)
  5. RANGE = 10000000
  6.  
  7. def _time_it(func):
  8.     def wrapper():
  9.         start_time = time.time()
  10.         try:
  11.             func()
  12.         except Exception:
  13.             raise
  14.         finally:
  15.             end_time = time.time()
  16.             total_time = end_time - start_time
  17.             minutes, seconds = divmod(total_time, 60)
  18.             print(f"Function '{func.__name__}' took {int(minutes)} "\
  19.                   f"minutes and {seconds:.2f} seconds to complete.")
  20.     return wrapper
  21.  
  22.  
  23. @_time_it
  24. def with_formatting():
  25.     for index in range(RANGE):
  26.         logging.debug(f"Script: {__name__} formatting index {index}")
  27.  
  28.  
  29. @_time_it
  30. def without_formatting():
  31.     for index in range(RANGE):
  32.         logging.debug("Script: %s formatting index %d", __name__, index)
  33.  
  34.  
  35. with_formatting()
  36. without_formatting()
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement