From c6b8b768a5c80adde2de81285963aea21da2e214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Heval=20S=C3=B6=C4=9F=C3=BCt?= <66115650+hevalsogut@users.noreply.github.com> Date: Sun, 16 Nov 2025 12:54:29 +0300 Subject: [PATCH] Create decorators_heval_sogut.py --- Week04/decorators_heval_sogut.py | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Week04/decorators_heval_sogut.py diff --git a/Week04/decorators_heval_sogut.py b/Week04/decorators_heval_sogut.py new file mode 100644 index 00000000..1be683f1 --- /dev/null +++ b/Week04/decorators_heval_sogut.py @@ -0,0 +1,39 @@ +import time +import tracemalloc + +def performance(func): + + # This is the wrapper function. + def wrapper(*args, **kwargs): + """Wrapper function that adds performance tracking.""" + + # --- "Before" logic --- + tracemalloc.start() + start_time = time.perf_counter() + + # Execute the original function + result = func(*args, **kwargs) + + # --- "After" logic --- + # Get performance metrics + end_time = time.perf_counter() + _current_mem, peak_mem = tracemalloc.get_traced_memory() + tracemalloc.stop() + + elapsed_time = end_time - start_time + + # Update the statistics + performance.counter += 1 + performance.total_time += elapsed_time + performance.total_mem += peak_mem + + return result + + # The decorator returns the new wrapper function + return wrapper + +# Initialize the state as attributes of the function object. +# This state will be shared across all uses of the @performance decorator. +performance.counter = 0 +performance.total_time = 0.0 +performance.total_mem = 0