From 6fc2496f9ec175a5a8e2ab9f4e3c52299610b2e1 Mon Sep 17 00:00:00 2001 From: omergzc Date: Fri, 5 Jun 2026 18:20:19 +0300 Subject: [PATCH] Create decorators_omer_gezici.py --- Week05/decorators_omer_gezici.py | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Week05/decorators_omer_gezici.py diff --git a/Week05/decorators_omer_gezici.py b/Week05/decorators_omer_gezici.py new file mode 100644 index 00000000..a39cbe02 --- /dev/null +++ b/Week05/decorators_omer_gezici.py @@ -0,0 +1,41 @@ +import time +import tracemalloc +from functools import wraps + +def performance(func): + """ + A decorator that measures the performance of a function and stores statistics. + + This decorator tracks the number of times the function is called, the total + execution time, and the peak memory consumption. These metrics are stored + as attributes on the wrapper function itself. + + :param func: The function to be decorated and measured. + :type func: callable + :return: The wrapper function that executes the original function and records metrics. + :rtype: callable + """ + @wraps(func) + def wrapper(*args, **kwargs): + + wrapper.counter += 1 + + tracemalloc.start() + start_time = time.perf_counter() + + result = func(*args, **kwargs) + + end_time = time.perf_counter() + current_mem, peak_mem = tracemalloc.get_traced_memory() + tracemalloc.stop() + + wrapper.total_time += (end_time - start_time) + wrapper.total_mem += peak_mem + + return result + + wrapper.counter = 0 + wrapper.total_time = 0.0 + wrapper.total_mem = 0 + + return wrapper