From 3941a5f5e128c290b28897487bcdf3b0a84a47c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Eekip=20Yaman=20Arslan?= <155294621+yamanarslan@users.noreply.github.com> Date: Tue, 7 Apr 2026 21:41:49 +0300 Subject: [PATCH] Implement performance decorator for function metrics Added a performance decorator to measure execution time and memory usage. --- Week04/decorators_sekip_yaman_arslan.py | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Week04/decorators_sekip_yaman_arslan.py diff --git a/Week04/decorators_sekip_yaman_arslan.py b/Week04/decorators_sekip_yaman_arslan.py new file mode 100644 index 00000000..267a4141 --- /dev/null +++ b/Week04/decorators_sekip_yaman_arslan.py @@ -0,0 +1,32 @@ +import time +import tracemalloc as tm + + +def performance(func): + """ + A decorator function that measures the performance of the given function + and also saves some statistics. + (The definition is from the lecture notes.) + """ + performance.counter = 0 + performance.total_mem = 0.0 + performance.total_time = 0.0 + + def _performance(*args, **kwargs): + tm.start() + start_time = time.time() + + result = func(*args, **kwargs) + + _, peak_mem = tm.get_traced_memory() + end_time = time.time() + + tm.stop() + + performance.total_time += end_time - start_time + performance.total_mem += peak_mem + performance.counter += 1 + + return result + + return _performance