From 1983233a61b5fba832e37d81e4751d82ef0c9a7c Mon Sep 17 00:00:00 2001 From: hasanberkesanal1 Date: Mon, 16 Nov 2026 20:32:28 +0300 Subject: [PATCH] Create decorators_hasan_berke_sanal.py --- Week04/decorators_hasan_berke_sanal.py | 58 ++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Week04/decorators_hasan_berke_sanal.py diff --git a/Week04/decorators_hasan_berke_sanal.py b/Week04/decorators_hasan_berke_sanal.py new file mode 100644 index 00000000..9d4514b8 --- /dev/null +++ b/Week04/decorators_hasan_berke_sanal.py @@ -0,0 +1,58 @@ +import functools +import time +import tracemalloc + + +def performance(fn): + """ + Decorator that measures execution time and memory of the wrapped function. + + Statistics are stored as attributes on the decorator itself: + + - ``performance.counter``: number of decorated-function calls + - ``performance.total_time``: total time spent in seconds + - ``performance.total_mem``: total memory consumed in bytes + + :param fn: the function to decorate + :type fn: callable + :return: the wrapped function + :rtype: callable + """ + + @functools.wraps(fn) + def wrapper(*args, **kwargs): + started_here = not tracemalloc.is_tracing() + if started_here: + tracemalloc.start() + tracemalloc.reset_peak() + mem_before = tracemalloc.get_traced_memory()[0] + start = time.perf_counter() + + try: + return fn(*args, **kwargs) + finally: + elapsed = time.perf_counter() - start + peak = tracemalloc.get_traced_memory()[1] + if started_here: + tracemalloc.stop() + performance.counter += 1 + performance.total_time += elapsed + performance.total_mem += max(peak - mem_before, 0) + + return wrapper + + +performance.counter = 0 +performance.total_time = 0.0 +performance.total_mem = 0 + + +if __name__ == "__main__": + + @performance + def build_list(n: int) -> list[int]: + return [i * i for i in range(n)] + + build_list(100_000) + build_list(50_000) + print(performance.counter, performance.total_time, performance.total_mem)