From 8b4c8f8954d3e619e101277ffeb0419d1c885a1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20=C3=87olako=C4=9Flu?= <130840093+mertcolakoglu@users.noreply.github.com> Date: Tue, 7 Apr 2026 22:22:20 +0300 Subject: [PATCH] Add performance decorator for function metrics This decorator tracks the execution time and memory usage of any function it wraps, providing metrics for performance evaluation. --- Week04/decorators_mert_colakoglu.py | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Week04/decorators_mert_colakoglu.py diff --git a/Week04/decorators_mert_colakoglu.py b/Week04/decorators_mert_colakoglu.py new file mode 100644 index 00000000..90c56033 --- /dev/null +++ b/Week04/decorators_mert_colakoglu.py @@ -0,0 +1,37 @@ +import time +import tracemalloc + +def performance(func): + """ + Evaluates the execution time and memory footprint of the function. + + :param func: Function to decorate. + :type func: callable + :return: Wrapped function. + :rtype: callable + + :cvar counter: Number of calls. + :cvar total_time: Total execution time in seconds. + :cvar total_mem: Total peak memory in bytes. + """ + def _performance(*args, **kwargs): + tracemalloc.start() + start_tick = time.perf_counter() + + output = func(*args, **kwargs) + + end_tick = time.perf_counter() + current_mem, peak_mem = tracemalloc.get_traced_memory() + tracemalloc.stop() + + performance.counter += 1 + performance.total_time += (end_tick - start_tick) + performance.total_mem += peak_mem + + return output + + return _performance + +performance.counter = 0 +performance.total_time = 0.0 +performance.total_mem = 0