Skip to content

Commit e977e52

Browse files
authored
Merge pull request #640 from erenmalkoc/patch-3
Create decorators_eren_malkoc.py
2 parents 2525dd9 + 3be4d3d commit e977e52

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Week04/decorators_eren_malkoc.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import time
2+
import tracemalloc
3+
4+
5+
def performance(fn):
6+
performance.counter = 0
7+
performance.total_time = 0.0
8+
performance.total_mem = 0
9+
10+
def wrapper(*args, **kwargs):
11+
12+
start_time = time.time()
13+
tracemalloc.start()
14+
15+
16+
result = fn(*args, **kwargs)
17+
18+
19+
end_time = time.time() - start_time
20+
current_mem, peak_mem = tracemalloc.get_traced_memory()
21+
22+
tracemalloc.stop()
23+
24+
25+
performance.counter += 1
26+
performance.total_time += end_time
27+
performance.total_mem += peak_mem
28+
29+
30+
print(f"Execution {performance.counter}:")
31+
print(f"Time: {end_time:.6f} seconds")
32+
print(f"Peak Memory Usage: {peak_mem / 1024:.2f} KB\n")
33+
34+
return result
35+
36+
return wrapper

0 commit comments

Comments
 (0)