Skip to content

Commit

Permalink
Create decorators_ata_ceyhun_alaca.py
Browse files Browse the repository at this point in the history
  • Loading branch information
AtaAlaca committed Nov 6, 2023
1 parent ad0c519 commit fc24617
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Week03/decorators_ata_ceyhun_alaca.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import time
import tracemalloc


def performance(func):
"""
:param func:
:return: A decorated function that measures performance.
"""
if not hasattr(performance, "counter"):
performance.counter = 0
performance.total_time = 0
performance.total_mem = 0

def decorated_function(*args, **kwargs):
start_time = time.perf_counter()
performance.counter += 1

tracemalloc.start()
result = func(*args, **kwargs)
end_time = time.perf_counter()

performance.total_mem += tracemalloc.get_traced_memory()[1]
tracemalloc.stop()
performance.total_time += (end_time - start_time)
return result

return decorated_function

0 comments on commit fc24617

Please sign in to comment.