From 7bd9b07cce6304b623d376005f3a806df2903d7b Mon Sep 17 00:00:00 2001 From: beytullaharslannn <151189674+beytullaharslannn@users.noreply.github.com> Date: Tue, 20 Oct 2026 13:17:52 +0300 Subject: [PATCH] Add performance decorator with memory and time tracking Implement a performance decorator to measure execution time and memory usage. --- Week04/decorators_beytullah_arslan.py | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Week04/decorators_beytullah_arslan.py diff --git a/Week04/decorators_beytullah_arslan.py b/Week04/decorators_beytullah_arslan.py new file mode 100644 index 00000000..3ac9f3fa --- /dev/null +++ b/Week04/decorators_beytullah_arslan.py @@ -0,0 +1,31 @@ +import time +import tracemalloc + +def performance(func): + if not hasattr(performance, 'counter'): + performance.counter = 0 + performance.total_time = 0.0 + performance.total_mem = 0.0 + def wrapper(*args, **kwargs): + # 1. Ölçümleri Başlat + tracemalloc.start() + start_time = time.perf_counter() + + # 2. Asıl Fonksiyonu Çalıştır + result = func(*args, **kwargs) + + # 3. Ölçümleri Bitir + end_time = time.perf_counter() + current_mem, peak_mem = tracemalloc.get_traced_memory() + tracemalloc.stop() + + # 4. İstatistikleri Güncelle + performance.counter += 1 + performance.total_time += (end_time - start_time) + performance.total_mem += peak_mem + + return result + wrapper.__name__ = func.__name__ + wrapper.__doc__ = func.__doc__ + + return wrapper