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
32 changes: 32 additions & 0 deletions Week04/decorators_berra_soyler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import time
import tracemalloc

def performance(func):
"""
A decorator that measures the performance of a function by tracking the
number of calls, total execution time, and peak memory usage.

Attributes:
- counter: Tracks the number of times the decorated function is called.
- total_time: Accumulates the total execution time across all calls.
- total_mem: Accumulates the peak memory usage across all calls.
"""
setattr(performance, 'counter', 0)
setattr(performance, 'total_time', 0.0)
setattr(performance, 'total_mem', 0.0)

def inner(*args, **kwargs):
tracemalloc.start()
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
current, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()

setattr(performance, 'counter', getattr(performance, 'counter') + 1)
setattr(performance, 'total_time', getattr(performance, 'total_time') + (end_time - start_time))
setattr(performance, 'total_mem', getattr(performance, 'total_mem') + peak)

return result

return inner
Loading