Skip to content

Instantly share code, notes, and snippets.

@maddrum
Created December 8, 2021 13:52
Show Gist options
  • Save maddrum/c135e46bfd5cabf63915391823e1638b to your computer and use it in GitHub Desktop.
Save maddrum/c135e46bfd5cabf63915391823e1638b to your computer and use it in GitHub Desktop.
import logging
import time
logger = logging.getLogger('MyLogger')
def measure_run_time(func):
def wrapper(*args, **kwargs):
total_start_time = time.time_ns()
cpu_start_time = time.process_time_ns()
result = func(*args, **kwargs)
cpu_elapsed_time = int((time.process_time_ns() - cpu_start_time) / 1_000_000)
total_time = int((time.time_ns() - total_start_time) / 1_000_000)
logger.info(f'[INFO][MEASURE] Function <{func.__name__}> ran for: \n'
f' CPU RUNTIME: {cpu_elapsed_time if cpu_elapsed_time > 1 else "< 1"} ms\n'
f' TOTAL RUNTIME: {total_time if total_time > 1 else "< 1"} ms \n')
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment