Skip to content

Instantly share code, notes, and snippets.

@davidbegin
Created January 21, 2020 03:43
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidbegin/d24e25847a952441b62583d973b6c62e to your computer and use it in GitHub Desktop.
Save davidbegin/d24e25847a952441b62583d973b6c62e to your computer and use it in GitHub Desktop.
Measure Memory usage of functions with a decorator
# Stolen from: https://medium.com/survata-engineering-blog/monitoring-memory-usage-of-a-running-python-program-49f027e3d1ba
print("\33c")
import tracemalloc
def measure_memory(func):
tracemalloc.start()
func()
current, peak = tracemalloc.get_traced_memory()
print(f"\n\033[37mFunction Name :\033[35;1m {func.__name__}\033[0m")
print(f"\033[37mCurrent memory usage:\033[36m {current / 10**6}MB\033[0m")
print(f"\033[37mPeak :\033[36m {peak / 10**6}MB\033[0m")
tracemalloc.stop()
@measure_memory
def method_1():
return list(range(1000000))
@measure_memory
def method_2():
return range(1000000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment