Skip to content

Instantly share code, notes, and snippets.

@vubon
Created February 18, 2022 03:39
Show Gist options
  • Save vubon/99c867db37e492e2175f2785e92bcefa to your computer and use it in GitHub Desktop.
Save vubon/99c867db37e492e2175f2785e92bcefa to your computer and use it in GitHub Desktop.
Get usage memory & execution time
import tracemalloc
from time import perf_counter
def performance(func):
def wrapper(*args, **kwargs):
tracemalloc.start()
start_time = perf_counter()
func(*args, **kwargs)
current, peak = tracemalloc.get_traced_memory()
finish_time = perf_counter()
print(f'Function: `{func.__name__}`')
print(f'Docs of `{func.__name__}`: {func.__doc__}')
print(f'Memory usage:\t\t {current / 10 ** 6:.6f} MB \n'
f'Peak memory usage:\t {peak / 10 ** 6:.6f} MB ')
print(f'Time elapsed is seconds: {finish_time - start_time:.6f}')
print(f'{"-" * 50}')
tracemalloc.stop()
return wrapper
@performance
def hello():
"""Hello docs"""
print("Hello world")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment