diff --git a/Week04/decorators_helin_harman.py b/Week04/decorators_helin_harman.py new file mode 100644 index 00000000..0a2bab8b --- /dev/null +++ b/Week04/decorators_helin_harman.py @@ -0,0 +1,29 @@ +import time +import tracemalloc +from functools import wraps + + +def performance(func): + @wraps(func) + def wrapper(*args, **kwargs): + wrapper.counter += 1 + + start_time = time.perf_counter() + tracemalloc.start() + + result = func(*args, **kwargs) + + current, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + end_time = time.perf_counter() + + wrapper.total_time += end_time - start_time + wrapper.total_mem += peak + + return result + + wrapper.counter = 0 + wrapper.total_time = 0.0 + wrapper.total_mem = 0 + + return wrapper