We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 71f5b39 commit 72a3977Copy full SHA for 72a3977
Week04/decorators_rafet_taskin.py
@@ -0,0 +1,27 @@
1
+import time
2
+import tracemalloc
3
+import functools
4
+
5
+def performance(func):
6
+ @functools.wraps(func) # Preserve metadata of the original function
7
+ def wrapper(*args, **kwargs):
8
+ tracemalloc.start()
9
+ start_time = time.time()
10
11
+ try:
12
+ result = func(*args, **kwargs)
13
+ finally:
14
+ current, peak = tracemalloc.get_traced_memory()
15
+ tracemalloc.stop()
16
+ end_time = time.time()
17
18
+ performance.counter += 1
19
+ performance.total_time += (end_time - start_time)
20
+ performance.total_mem += peak
21
22
+ return result
23
+ return wrapper
24
25
+performance.counter = 0
26
+performance.total_time = 0
27
+performance.total_mem = 0
0 commit comments