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


def performance(func):
"""
A decorator function that measures the performance of the given function
and also saves some statistics.
(The definition is from the lecture notes.)
"""
performance.counter = 0
performance.total_mem = 0.0
performance.total_time = 0.0

def _performance(*args, **kwargs):
tm.start()
start_time = time.time()

result = func(*args, **kwargs)

_, peak_mem = tm.get_traced_memory()
end_time = time.time()

tm.stop()

performance.total_time += end_time - start_time
performance.total_mem += peak_mem
performance.counter += 1

return result

return _performance