From 79678eea62c69f34a05231ec37b7fc8792d8be18 Mon Sep 17 00:00:00 2001 From: Kerem Kurtuldu <200315061@ogr.cbu.edu.tr> Date: Mon, 6 Jan 2025 21:57:46 +0300 Subject: [PATCH] Create decorators_kerem_kurtuldu.py --- Week04/decorators_kerem_kurtuldu.py | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Week04/decorators_kerem_kurtuldu.py diff --git a/Week04/decorators_kerem_kurtuldu.py b/Week04/decorators_kerem_kurtuldu.py new file mode 100644 index 00000000..14b9bac2 --- /dev/null +++ b/Week04/decorators_kerem_kurtuldu.py @@ -0,0 +1,36 @@ +import time +import tracemalloc + +def measure_performance(func): + # Initialize performance counters + measure_performance.call_count = 0 + measure_performance.total_execution_time = 0.0 + measure_performance.total_peak_memory = 0 + + def wrapper(*args, **kwargs): + """Wrapper to measure performance of the decorated function.""" + # Start timing and memory tracking + start_time = time.perf_counter() # More precise than time.time() + tracemalloc.start() # Start memory tracking + + # Execute the actual function + result = func(*args, **kwargs) + + # Measure time and memory usage + execution_time = time.perf_counter() - start_time + current_memory, peak_memory = tracemalloc.get_traced_memory() + tracemalloc.stop() # Stop memory tracking + + # Update cumulative stats + measure_performance.call_count += 1 + measure_performance.total_execution_time += execution_time + measure_performance.total_peak_memory += peak_memory + + # Display performance metrics + print(f"Call #{measure_performance.call_count}:") + print(f"Execution Time: {execution_time:.6f} seconds") + print(f"Peak Memory Usage: {peak_memory / 1024:.2f} KB\n") + + return result + + return wrapper