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

def performance(func):
"""
Evaluates the execution time and memory footprint of the function.

:param func: Function to decorate.
:type func: callable
:return: Wrapped function.
:rtype: callable

:cvar counter: Number of calls.
:cvar total_time: Total execution time in seconds.
:cvar total_mem: Total peak memory in bytes.
"""
def _performance(*args, **kwargs):
tracemalloc.start()
start_tick = time.perf_counter()

output = func(*args, **kwargs)

end_tick = time.perf_counter()
current_mem, peak_mem = tracemalloc.get_traced_memory()
tracemalloc.stop()

performance.counter += 1
performance.total_time += (end_tick - start_tick)
performance.total_mem += peak_mem

return output

return _performance

performance.counter = 0
performance.total_time = 0.0
performance.total_mem = 0