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.
2 parents 0ad2d09 + 80ca90f commit c9783aeCopy full SHA for c9783ae
Week04/decorators_beyza_saridas.py
@@ -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