From e5b5a296a3922be69ab7791ec19e755243986c94 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: Sat, 20 Dec 2025 16:11:22 +0300 Subject: [PATCH 1/3] Create decorators_fevzi_bagriacik.py --- Week04/decorators_fevzi_bagriacik.py | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 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..c42b1265 --- /dev/null +++ b/Week04/decorators_fevzi_bagriacik.py @@ -0,0 +1,33 @@ +import time +import tracemalloc +from functools import wraps + + +def performance(func): + """ + A decorator which measures the performance of functions and + also saves some statistics. + """ + + @wraps(func) + def wrapper(*args, **kwargs): + tracemalloc.start() + start_time = time.perf_counter() + + result = func(*args, **kwargs) + + end_time = time.perf_counter() + current, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + + wrapper.counter += 1 + wrapper.total_time += (end_time - start_time) + wrapper.total_mem += peak + + return result + + wrapper.counter = 0 + wrapper.total_time = 0.0 + wrapper.total_mem = 0 + + return wrapper From 3feefaddde0d3d1e700b0d770905a294f924f58e 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: Sat, 20 Dec 2025 16:14:48 +0300 Subject: [PATCH 2/3] Update decorators_fevzi_bagriacik.py --- Week04/decorators_fevzi_bagriacik.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Week04/decorators_fevzi_bagriacik.py b/Week04/decorators_fevzi_bagriacik.py index c42b1265..67f7afd4 100644 --- a/Week04/decorators_fevzi_bagriacik.py +++ b/Week04/decorators_fevzi_bagriacik.py @@ -1,33 +1,33 @@ import time import tracemalloc -from functools import wraps +from functools import update_wrapper -def performance(func): +class performance: """ A decorator which measures the performance of functions and also saves some statistics. """ - @wraps(func) - def wrapper(*args, **kwargs): + def __init__(self, func): + self.func = func + self.counter = 0 + self.total_time = 0.0 + self.total_mem = 0 + update_wrapper(self, func) + + def __call__(self, *args, **kwargs): tracemalloc.start() start_time = time.perf_counter() - result = func(*args, **kwargs) + result = self.func(*args, **kwargs) end_time = time.perf_counter() current, peak = tracemalloc.get_traced_memory() tracemalloc.stop() - wrapper.counter += 1 - wrapper.total_time += (end_time - start_time) - wrapper.total_mem += peak + self.counter += 1 + self.total_time += (end_time - start_time) + self.total_mem += peak return result - - wrapper.counter = 0 - wrapper.total_time = 0.0 - wrapper.total_mem = 0 - - return wrapper From bdaa9718ab7cd815648ba8a0b3c2b788eee2a97a 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: Sat, 20 Dec 2025 16:16:35 +0300 Subject: [PATCH 3/3] Update decorators_fevzi_bagriacik.py --- Week04/decorators_fevzi_bagriacik.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/Week04/decorators_fevzi_bagriacik.py b/Week04/decorators_fevzi_bagriacik.py index 67f7afd4..fb34afd8 100644 --- a/Week04/decorators_fevzi_bagriacik.py +++ b/Week04/decorators_fevzi_bagriacik.py @@ -1,20 +1,16 @@ import time import tracemalloc -from functools import update_wrapper +from functools import wraps class performance: - """ - A decorator which measures the performance of functions and - also saves some statistics. - """ + 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 - update_wrapper(self, func) + wraps(func)(self) def __call__(self, *args, **kwargs): tracemalloc.start() @@ -26,8 +22,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