Skip to content

Instantly share code, notes, and snippets.

@dmmeteo
Forked from vald-phoenix/mem.py
Created Jul 15, 2020
Embed
What would you like to do?
A dummy decorator function to measure memory usage of a Python function, method, etc.
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