Created
January 21, 2020 03:43
-
-
Save davidbegin/d24e25847a952441b62583d973b6c62e to your computer and use it in GitHub Desktop.
Measure Memory usage of functions with a decorator
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
# 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