Skip to content

Instantly share code, notes, and snippets.

@arvind-iyer
Created November 7, 2019 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arvind-iyer/d61918e1eb4a1c510f701155e4c42e83 to your computer and use it in GitHub Desktop.
Save arvind-iyer/d61918e1eb4a1c510f701155e4c42e83 to your computer and use it in GitHub Desktop.
Benchmark function
# Function decorator to save system CPU and memory consumption
# as well as execution duration of the function supplied
import time
import psutil
def timeit(report):
"""
Use as decorator for a function, provide a dictionary object as parameter
"""
def timef(method):
def timed(*args, **kwargs):
ts = time.time()
result = method(*args, **kwargs)
te = time.time()
system_status = {
"time": (te - ts),
"cpu": psutil.cpu_percent(),
"mem": psutil.virtual_memory()[3] / 2.**30
}
report[method.__name__] = system_status
return result
return timed
return timef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment