Skip to content

Commit 327da6c

Browse files
authored
Create decorators_rukiye_ilhan.py
1 parent fc02709 commit 327da6c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Week04/decorators_rukiye_ilhan.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import tracemalloc
2+
import time
3+
4+
5+
def performance_decoraters(func):
6+
if not hasattr(performance_decoraters ,'counter'):
7+
performance_decoraters.counter = 0
8+
performance_decoraters.total_time = 0
9+
performance_decoraters.total_mem = 0
10+
11+
def perform(*args,**kwargs):
12+
tracemalloc.start() # start to follow memory
13+
start_time = time.time()
14+
result = func(*args, **kwargs)
15+
end_time = time.time()
16+
used_memory = tracemalloc.get_traced_memory()[1] # [0] gives current memory consumption but [1] gives total(max) memory consumption during thr last fallowing time
17+
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
18+
performance_decoraters.counter += 1
19+
performance_decoraters.total_time += end_time - start_time
20+
performance_decoraters.total_mem += used_memory
21+
return result
22+
return perform

0 commit comments

Comments
 (0)