Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8faeb84

Browse filesBrowse files
Add small utility to profile any function
1 parent 71efa79 commit 8faeb84
Copy full SHA for 8faeb84

File tree

Expand file treeCollapse file tree

2 files changed

+30
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+30
-0
lines changed
Open diff view settings
Collapse file

‎profiling/profiling.py‎

Copy file name to clipboard
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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
Collapse file

‎test_requirements.txt‎

Copy file name to clipboardExpand all lines: test_requirements.txt
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ coverage>=6.4.4
77
flake8>=3.9.2
88
isort>=5.8.0
99
mypy>=0.740
10+
pyinstrument>=4.4.0

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.