From d094387a82bbd1923b41fdbed6f6a683af527e27 Mon Sep 17 00:00:00 2001 From: Sergenops Date: Mon, 9 Nov 2026 18:53:45 +0300 Subject: [PATCH] Add monitoring decorator for performance tracking This decorator monitors the execution time and memory usage of a function. --- Week04/decorators_sergen_maksutlar.py | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Week04/decorators_sergen_maksutlar.py diff --git a/Week04/decorators_sergen_maksutlar.py b/Week04/decorators_sergen_maksutlar.py new file mode 100644 index 00000000..d1293ba3 --- /dev/null +++ b/Week04/decorators_sergen_maksutlar.py @@ -0,0 +1,36 @@ +import time +import tracemalloc + + +def monitor(func): + + def execute(*args, **kwargs): + + tracemalloc.start() + + initial_time = time.perf_counter() + initial_memory = tracemalloc.get_traced_memory()[0] + + tracemalloc.reset_peak() + + try: + output = func(*args, **kwargs) + + finally: + elapsed_time = time.perf_counter() - initial_time + used_memory, max_memory = tracemalloc.get_traced_memory() + + monitor.calls += 1 + monitor.duration += elapsed_time + monitor.memory_usage += max_memory - initial_memory + + tracemalloc.stop() + + return output + + return execute + + +monitor.calls = 0 +monitor.duration = 0 +monitor.memory_usage = 0