-
-
Save dmmeteo/b2b92cae708805efe21d61916d4ec11c to your computer and use it in GitHub Desktop.
A dummy decorator function to measure memory usage of a Python function, method, etc.
This file contains hidden or 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
import resource | |
from functools import wraps | |
def mem_it(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
print( | |
'Pre-memory usage: {} (mb)'.format( | |
resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024 | |
) | |
) | |
f = func(*args, **kwargs) | |
print( | |
'Post-memory usage: {} (mb)'.format( | |
resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024 | |
) | |
) | |
return f | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment