From 314704c47e904779b32bd5139ead1315d98731ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mustafa=20Furkan=20Y=C4=B1lmaz?= Date: Wed, 7 Jan 2026 18:22:09 +0300 Subject: [PATCH] Decorator for function metrics --- Week04/decorators_mustafafurkan_yilmaz.py | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Week04/decorators_mustafafurkan_yilmaz.py diff --git a/Week04/decorators_mustafafurkan_yilmaz.py b/Week04/decorators_mustafafurkan_yilmaz.py new file mode 100644 index 00000000..3a195c8f --- /dev/null +++ b/Week04/decorators_mustafafurkan_yilmaz.py @@ -0,0 +1,46 @@ +import time +import tracemalloc +import functools + +def performance(func): + """ + A decorator that measures the execution time, memory usage, + and call count of the decorated functions. + """ + @functools.wraps(func) + def wrapper(*args, **kwargs): + # Increment the global counter for the decorator + performance.counter += 1 + + # Start tracking memory + tracemalloc.start() + # Start tracking time + start_time = time.perf_counter() + + try: + # Execute the actual function + result = func(*args, **kwargs) + finally: + # Calculate elapsed time + end_time = time.perf_counter() + elapsed_time = end_time - start_time + + # Get memory usage (current, peak) + current_mem, peak_mem = tracemalloc.get_traced_memory() + + # Stop tracking memory + tracemalloc.stop() + + # Update global stats + performance.total_time += elapsed_time + performance.total_mem += peak_mem + + return result + + return wrapper + +# Initialize the static attributes on the function object itself +# This allows all decorated functions to share these counters. +performance.counter = 0 +performance.total_time = 0 +performance.total_mem = 0