Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Week04/decorators_yagmur_tokdemir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import tracemalloc,time


def performance(f):
"""
This function is a decorator used to measure the performance of a function.

Attributes:
counter (int): The number of times the decorated function has been called.
total_mem (int): The total peak memory usage across all calls to the decorated function.
total_time (float): The total time taken by all calls to the decorated function in seconds.

Args:
f (function): The function to be decorated.

Returns:
function: The wrapper function that performs the tracking and calls the original function.

"""

setattr(performance, 'counter', 0)
setattr(performance, 'total_mem', 0)
setattr(performance, 'total_time', 0)

def wrapper(*args, **kwargs):
tracemalloc.start()
start_time = time.time()
result = f(*args, **kwargs)
end_time = time.time()
current, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
setattr(performance, 'counter', getattr(performance, 'counter') + 1)
setattr(performance, 'total_mem', getattr(performance, 'total_mem') + peak)
setattr(performance, 'total_time', getattr(performance, 'total_time') + end_time - start_time)

return result

return wrapper
Loading