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
38 changes: 16 additions & 22 deletions Week04/decorators_sema_erol.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
import time
import tracemalloc
import time

def performance(fn):
def _performance(*args, **kwargs):
# If the 'performance' function has not been called before, initialize static variables
if not hasattr(performance, "counter"):
setattr(performance, "counter", 0)
setattr(performance, "total_time", 0)
setattr(performance, "total_mem", 0)

performance.counter += 1

tracemalloc.start()
def performance(func):
if not hasattr(performance,'counter'):
performance.counter = 0
performance.total_time = 0
performance.total_mem = 0

def perform(*args,**kwargs):
tracemalloc.start() # start to follow memory
start_time = time.time()

result = fn(*args, **kwargs)

result = func(*args, **kwargs)
end_time = time.time()
current, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()

performance.total_mem += peak
performance.total_time += (end_time - start_time)

used_memory = tracemalloc.get_traced_memory()[1] # [0] gives current memory consumption but [1] gives total(max) memory consumption during thr last fallowing time
tracemalloc.stop() # we started memeory following process only to find how much memory uses by thr called function not the entire program ,so we stop memory following when the jop of function is finished
performance.counter += 1
performance.total_time += end_time - start_time
performance.total_mem += used_memory
return result

return _performance
return perform
Loading