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 e5b5a29Copy full SHA for e5b5a29
Week04/decorators_fevzi_bagriacik.py
@@ -0,0 +1,33 @@
1
+import time
2
+import tracemalloc
3
+from functools import wraps
4
+
5
6
+def performance(func):
7
+ """
8
+ A decorator which measures the performance of functions and
9
+ also saves some statistics.
10
11
12
+ @wraps(func)
13
+ def wrapper(*args, **kwargs):
14
+ tracemalloc.start()
15
+ start_time = time.perf_counter()
16
17
+ result = func(*args, **kwargs)
18
19
+ end_time = time.perf_counter()
20
+ current, peak = tracemalloc.get_traced_memory()
21
+ tracemalloc.stop()
22
23
+ wrapper.counter += 1
24
+ wrapper.total_time += (end_time - start_time)
25
+ wrapper.total_mem += peak
26
27
+ return result
28
29
+ wrapper.counter = 0
30
+ wrapper.total_time = 0.0
31
+ wrapper.total_mem = 0
32
33
+ return wrapper
0 commit comments