Created
November 7, 2019 05:08
-
-
Save arvind-iyer/d61918e1eb4a1c510f701155e4c42e83 to your computer and use it in GitHub Desktop.
Benchmark function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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