File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ import functools
2
+
3
+ from pyinstrument .profiler import Profiler
4
+
5
+
6
+ def profile_function (output_file = "profile.html" ):
7
+ """
8
+ Profiles a function execution time.
9
+
10
+ Parameters
11
+ ----------
12
+ output_file: file to write profile output. Defaults to "profile.html".
13
+ """
14
+
15
+ def decorator (function ):
16
+ @functools .wraps (function )
17
+ def wrapper (* args , ** kwargs ):
18
+ profiler = Profiler ()
19
+ profiler .start ()
20
+ result = function (* args , ** kwargs )
21
+ profiler .stop ()
22
+ output = profiler .output_html ()
23
+ with open (output_file , "w" ) as f :
24
+ f .write (output )
25
+ return result
26
+
27
+ return wrapper
28
+
29
+ return decorator
Original file line number Diff line number Diff line change @@ -7,3 +7,4 @@ coverage>=6.4.4
7
7
flake8>=3.9.2
8
8
isort>=5.8.0
9
9
mypy>=0.740
10
+ pyinstrument>=4.4.0
You can’t perform that action at this time.
0 commit comments