From bf9ac03fee8ea3f5fb7a1dabef1ec29d7ddbd48f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Hakan=20Y=C4=B1ld=C4=B1r=C4=B1m?= Date: Tue, 31 Mar 2026 22:03:10 +0300 Subject: [PATCH 1/2] Refactor performance decorator for clarity and efficiency --- Week04/decorators_ahmet_hakan_yildirim.py | 34 +++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Week04/decorators_ahmet_hakan_yildirim.py diff --git a/Week04/decorators_ahmet_hakan_yildirim.py b/Week04/decorators_ahmet_hakan_yildirim.py new file mode 100644 index 00000000..61f804ef --- /dev/null +++ b/Week04/decorators_ahmet_hakan_yildirim.py @@ -0,0 +1,34 @@ +import time,tracemalloc + + +def performance(fn): + """Decorator that tracks execution time and memory consumption of a function.""" + # Initialize tracking attributes if not already present + if not hasattr(performance, "call_count"): + performance.call_count = 0 + performance.time_elapsed = 0 + performance.memory_used = 0 + + def inner(*args, **kwargs): + # Update function invocation count + performance.call_count += 1 + + # Begin memory and execution time monitoring + tracemalloc.start() + start = time.time() + + # Run the original function + ret_value = fn(*args, **kwargs) + + # End monitoring and capture metrics + end = time.time() + current_usage, max_usage = tracemalloc.get_traced_memory() + tracemalloc.stop() + + # Accumulate memory and execution time + performance.memory_used += max_usage + performance.time_elapsed += (end - start) + + return ret_value + + return inner From 7f64788a5df276a4a641f3a35ce518bce75f821a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Hakan=20Y=C4=B1ld=C4=B1r=C4=B1m?= Date: Tue, 31 Mar 2026 22:07:06 +0300 Subject: [PATCH 2/2] Refactor performance decorator for clarity and accuracy --- Week04/decorators_ahmet_hakan_yildirim.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Week04/decorators_ahmet_hakan_yildirim.py b/Week04/decorators_ahmet_hakan_yildirim.py index 61f804ef..2898bac9 100644 --- a/Week04/decorators_ahmet_hakan_yildirim.py +++ b/Week04/decorators_ahmet_hakan_yildirim.py @@ -1,17 +1,17 @@ -import time,tracemalloc - +import time +import tracemalloc def performance(fn): """Decorator that tracks execution time and memory consumption of a function.""" # Initialize tracking attributes if not already present - if not hasattr(performance, "call_count"): - performance.call_count = 0 - performance.time_elapsed = 0 - performance.memory_used = 0 + if not hasattr(performance, "counter"): + performance.counter = 0 + performance.total_time = 0 + performance.total_mem = 0 def inner(*args, **kwargs): # Update function invocation count - performance.call_count += 1 + performance.counter += 1 # Begin memory and execution time monitoring tracemalloc.start() @@ -26,8 +26,8 @@ def inner(*args, **kwargs): tracemalloc.stop() # Accumulate memory and execution time - performance.memory_used += max_usage - performance.time_elapsed += (end - start) + performance.total_mem += max_usage + performance.total_time += (end - start) return ret_value