From 479626d53447ad7170d67cc1a4ff63cf5c11a980 Mon Sep 17 00:00:00 2001 From: hatice <1akgullhaticee@gmail.com> Date: Wed, 7 Jan 2026 19:00:12 +0300 Subject: [PATCH] Create decorators_hatice_akgul.py --- Week04/decorators_hatice_akgul.py | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Week04/decorators_hatice_akgul.py diff --git a/Week04/decorators_hatice_akgul.py b/Week04/decorators_hatice_akgul.py new file mode 100644 index 00000000..78b4279d --- /dev/null +++ b/Week04/decorators_hatice_akgul.py @@ -0,0 +1,34 @@ +import time +import tracemalloc +import functools + +def performance(func): + """ + Fonksiyonun performansını ölçen ve istatistiklerini saklayan dekoratör. + """ + if not hasattr(performance, "counter"): + performance.counter = 0 + performance.total_time = 0.0 + performance.total_mem = 0 + + @functools.wraps(func) + def wrapper(*args, **kwargs): + 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() + + performance.counter += 1 + performance.total_time += (end_time - start_time) + performance.total_mem += peak_mem + + return result + + return wrapper + +performance.counter = 0 +performance.total_time = 0.0 +performance.total_mem = 0