Skip to content

Commit c9783ae

Browse files
authored
Merge pull request #618 from beyzasaridas/patch-5
Create decorators_beyza_saridas.py
2 parents 0ad2d09 + 80ca90f commit c9783ae

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Week04/decorators_beyza_saridas.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import time
2+
import tracemalloc
3+
4+
def performance(fn):
5+
def _performance(*args, **kwargs):
6+
# If the 'performance' function has not been called before, initialize static variables
7+
if not hasattr(performance, "counter"):
8+
setattr(performance, "counter", 0)
9+
setattr(performance, "total_time", 0)
10+
setattr(performance, "total_mem", 0)
11+
12+
performance.counter += 1
13+
14+
tracemalloc.start()
15+
start_time = time.time()
16+
17+
result = fn(*args, **kwargs)
18+
19+
end_time = time.time()
20+
current, peak = tracemalloc.get_traced_memory()
21+
tracemalloc.stop()
22+
23+
performance.total_mem += peak
24+
performance.total_time += (end_time - start_time)
25+
26+
return result
27+
28+
return _performance

0 commit comments

Comments
 (0)