File tree 1 file changed +36
-0
lines changed
1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ import time
2
+ import tracemalloc
3
+
4
+ def performance (func ):
5
+ setattr (performance , '_count' , 0 )
6
+ setattr (performance , '_time' , 0.0 )
7
+ setattr (performance , '_memory' , 0.0 )
8
+
9
+ def wrapper (* args , ** kwargs ):
10
+ count = getattr (performance , '_count' ) + 1
11
+ setattr (performance , '_count' , count )
12
+
13
+ start = time .time ()
14
+ tracemalloc .start ()
15
+
16
+ try :
17
+ func (* args , ** kwargs )
18
+ except Exception as exc :
19
+ print (f"Error: { exc } " )
20
+ finally :
21
+ end = time .time ()
22
+ curr , peak = tracemalloc .get_traced_memory ()
23
+ tracemalloc .stop ()
24
+
25
+ elapsed = end - start
26
+ total_time = getattr (performance , '_time' ) + elapsed
27
+ total_mem = getattr (performance , '_memory' ) + peak
28
+
29
+ setattr (performance , '_time' , total_time )
30
+ setattr (performance , '_memory' , total_mem )
31
+
32
+ print (f"{ func .__name__ } : Calls={ count } , Time={ elapsed :.4f} s, "
33
+ f"Memory={ peak / 1024 :.1f} KB, TotalTime={ total_time :.4f} s, "
34
+ f"TotalMem={ total_mem / 1024 :.1f} KB" )
35
+
36
+ return wrapper
You can’t perform that action at this time.
0 commit comments