File tree 1 file changed +34
-0
lines changed
1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ import time
2
+ import tracemalloc
3
+
4
+ def performance (fn ):
5
+ """A decorator to measure the performance (time and memory usage) of a function."""
6
+ # Static variables for performance tracking
7
+ if not hasattr (performance , "counter" ):
8
+ performance .counter = 0
9
+ performance .total_time = 0
10
+ performance .total_mem = 0
11
+
12
+ def wrapper (* args , ** kwargs ):
13
+ # Increment the call counter
14
+ performance .counter += 1
15
+
16
+ # Start tracking memory and time
17
+ tracemalloc .start ()
18
+ start_time = time .time ()
19
+
20
+ # Execute the decorated function
21
+ result = fn (* args , ** kwargs )
22
+
23
+ # Stop tracking memory and calculate elapsed time
24
+ end_time = time .time ()
25
+ current , peak = tracemalloc .get_traced_memory ()
26
+ tracemalloc .stop ()
27
+
28
+ # Update total memory and time
29
+ performance .total_mem += peak
30
+ performance .total_time += (end_time - start_time )
31
+
32
+ return result
33
+
34
+ return wrapper
You can’t perform that action at this time.
0 commit comments