-
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add small utility to profile any function
- Loading branch information
1 parent
71efa79
commit 8faeb84
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import functools | ||
|
||
from pyinstrument.profiler import Profiler | ||
|
||
|
||
def profile_function(output_file="profile.html"): | ||
""" | ||
Profiles a function execution time. | ||
Parameters | ||
---------- | ||
output_file: file to write profile output. Defaults to "profile.html". | ||
""" | ||
|
||
def decorator(function): | ||
@functools.wraps(function) | ||
def wrapper(*args, **kwargs): | ||
profiler = Profiler() | ||
profiler.start() | ||
result = function(*args, **kwargs) | ||
profiler.stop() | ||
output = profiler.output_html() | ||
with open(output_file, "w") as f: | ||
f.write(output) | ||
return result | ||
|
||
return wrapper | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ coverage>=6.4.4 | |
flake8>=3.9.2 | ||
isort>=5.8.0 | ||
mypy>=0.740 | ||
pyinstrument>=4.4.0 |