Skip to content
Closed
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
34 changes: 34 additions & 0 deletions Week04/Week04/decorators_bredikhin_illia.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import time
import tracemalloc


def track_performance(func):
"""
Decorator for measuring execution stats of a function.
"""

stats = {
"calls": 0,
"time_sum": 0.0,
"memory_sum": 0
}

def inner(*args, **kwargs):
tracemalloc.start()

start = time.perf_counter()
result = func(*args, **kwargs)
duration = time.perf_counter() - start

_, peak_memory = tracemalloc.get_traced_memory()
tracemalloc.stop()

stats["calls"] += 1
stats["time_sum"] += duration
stats["memory_sum"] += peak_memory

return result

inner.stats = stats

return inner
Loading