From 69565df2982b6240056e1f791fb8dd18ce8003e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fevzi=20Ba=C4=9Fr=C4=B1a=C3=A7=C4=B1k?= <124450541+fevzibagriacik@users.noreply.github.com> Date: Thu, 2 Apr 2026 20:26:07 +0300 Subject: [PATCH 1/2] Add performance decorator for function metrics Implement a performance decorator to measure execution time and memory usage. --- Week04/decorators_fevzi_bagriacik.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Week04/decorators_fevzi_bagriacik.py diff --git a/Week04/decorators_fevzi_bagriacik.py b/Week04/decorators_fevzi_bagriacik.py new file mode 100644 index 00000000..e52ec2f8 --- /dev/null +++ b/Week04/decorators_fevzi_bagriacik.py @@ -0,0 +1,28 @@ +import time +import tracemalloc +from functools import wraps + +class performance: + def __init__(self, func): + self.func = func + self.counter = 0 + self.total_time = 0.0 + self.total_mem = 0 + + wraps(func)(self) + + def __call__(self, *args, **kwargs): + tracemalloc.start() + start_time = time.perf_counter() + + result = self.func(*args, **kwargs) + + end_time = time.perf_counter() + current, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + + self.counter += 1 + self.total_time += (end_time - start_time) + self.total_mem += peak + + return result From e170f19a2dc78793ee7541c1c86f126b4e2e9252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fevzi=20Ba=C4=9Fr=C4=B1a=C3=A7=C4=B1k?= <124450541+fevzibagriacik@users.noreply.github.com> Date: Thu, 2 Apr 2026 20:28:29 +0300 Subject: [PATCH 2/2] Update decorators_fevzi_bagriacik.py --- Week04/decorators_fevzi_bagriacik.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Week04/decorators_fevzi_bagriacik.py b/Week04/decorators_fevzi_bagriacik.py index e52ec2f8..31fb1b8a 100644 --- a/Week04/decorators_fevzi_bagriacik.py +++ b/Week04/decorators_fevzi_bagriacik.py @@ -3,12 +3,12 @@ from functools import wraps class performance: + counter = 0 + total_time = 0.0 + total_mem = 0 + def __init__(self, func): self.func = func - self.counter = 0 - self.total_time = 0.0 - self.total_mem = 0 - wraps(func)(self) def __call__(self, *args, **kwargs): @@ -21,8 +21,8 @@ def __call__(self, *args, **kwargs): current, peak = tracemalloc.get_traced_memory() tracemalloc.stop() - self.counter += 1 - self.total_time += (end_time - start_time) - self.total_mem += peak + performance.counter += 1 + performance.total_time += (end_time - start_time) + performance.total_mem += peak return result