From bc37706cae5b7fb0b96042187c04d3479d26513e Mon Sep 17 00:00:00 2001 From: Katex <67821908+katex35@users.noreply.github.com> Date: Fri, 11 Oct 2024 18:03:28 +0300 Subject: [PATCH 1/2] Create decorators_gokay_sahin.py --- Week04/decorators_gokay_sahin.py | 52 ++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Week04/decorators_gokay_sahin.py diff --git a/Week04/decorators_gokay_sahin.py b/Week04/decorators_gokay_sahin.py new file mode 100644 index 00000000..0a5dea57 --- /dev/null +++ b/Week04/decorators_gokay_sahin.py @@ -0,0 +1,52 @@ +import time +import psutil + +total_time = 0 +total_mem = 0 +counter = 0 + +def performance(fn): + """ + A decorator function to measure the performance of a given function by tracking + both its execution time and memory usage. + + This decorator calculates the time taken for the function to run and the + memory used before and after its execution. It also maintains a global counter + of how many times the decorated function has been called, along with + accumulating the total time and memory usage. + + :param fn: Function to be decorated and monitored. + :type fn: Callable + :return: A wrapper function that monitors performance of the original function. + :rtype: Callable + + Global Variables: + - **total_time** (float): Accumulates the total execution time across all invocations. + - **total_mem** (int): Accumulates the total memory difference across all invocations. + - **counter** (int): Tracks how many times the decorated function has been executed. + + Example usage:: + + @performance + def my_function(): + pass + + my_function() + """ + def wrapper(*args, **kwargs): + global total_time, total_mem, counter + process = psutil.Process() + + mem_before = process.memory_info().rss + time_before = time.time() + + counter += 1 + fn(*args, **kwargs) + + time_after = time.time() + total_time += time_after - time_before + + mem_after = process.memory_info().rss + total_mem += mem_after - mem_before + + return wrapper From dbf9da0ff626ef777ec8639fd92d9e05d7105145 Mon Sep 17 00:00:00 2001 From: Katex <67821908+katex35@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:51:58 +0300 Subject: [PATCH 2/2] Update decorators_gokay_sahin.py --- Week04/decorators_gokay_sahin.py | 66 +++++++++++++------------------- 1 file changed, 27 insertions(+), 39 deletions(-) diff --git a/Week04/decorators_gokay_sahin.py b/Week04/decorators_gokay_sahin.py index 0a5dea57..ccbe8f43 100644 --- a/Week04/decorators_gokay_sahin.py +++ b/Week04/decorators_gokay_sahin.py @@ -1,52 +1,40 @@ import time -import psutil - -total_time = 0 -total_mem = 0 -counter = 0 +import tracemalloc def performance(fn): """ - A decorator function to measure the performance of a given function by tracking - both its execution time and memory usage. - - This decorator calculates the time taken for the function to run and the - memory used before and after its execution. It also maintains a global counter - of how many times the decorated function has been called, along with - accumulating the total time and memory usage. - - :param fn: Function to be decorated and monitored. - :type fn: Callable - :return: A wrapper function that monitors performance of the original function. - :rtype: Callable - - Global Variables: - - **total_time** (float): Accumulates the total execution time across all invocations. - - **total_mem** (int): Accumulates the total memory difference across all invocations. - - **counter** (int): Tracks how many times the decorated function has been executed. - - Example usage:: - - @performance - def my_function(): - pass - - my_function() - """ + Decorator to measure performance of a function, including + execution time and memory usage. + + This decorator tracks: + - The number of times the function is called. + - The cumulative time taken by the function. + - The cumulative peak memory usage. + + :param fn: The function to be wrapped and measured. + :return: The result of the wrapped function. + """ + if not hasattr(performance, 'counter'): + performance.counter = 0 + performance.total_time = 0 + performance.total_mem = 0 + def wrapper(*args, **kwargs): - global total_time, total_mem, counter - process = psutil.Process() + tracemalloc.start() - mem_before = process.memory_info().rss time_before = time.time() - counter += 1 - fn(*args, **kwargs) + result = fn(*args, **kwargs) time_after = time.time() - total_time += time_after - time_before - mem_after = process.memory_info().rss - total_mem += mem_after - mem_before + curr_mem, peak_mem = tracemalloc.get_traced_memory() + tracemalloc.stop() + + performance.counter += 1 + performance.total_time += (time_after - time_before) + performance.total_mem += peak_mem + + return result return wrapper