Created
September 20, 2018 14:49
-
-
Save dcragusa/2cc47d59e68b402b8db591138468066b to your computer and use it in GitHub Desktop.
A line by line profiler for Python
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
# Adapted from https://zapier.com/engineering/profiling-python-boss/ | |
# Requires https://pypi.org/project/line_profiler/ | |
def profile_func(follow=[]): | |
from line_profiler import LineProfiler | |
# simply decorate the function you want profiled | |
# add any functions called you want followed to [] | |
def inner(func): | |
def profiled_func(*args, **kwargs): | |
profiler = LineProfiler() | |
try: | |
profiler.add_function(func) | |
for f in follow: | |
profiler.add_function(f) | |
profiler.enable_by_count() | |
return func(*args, **kwargs) | |
finally: | |
profiler.print_stats() | |
return profiled_func | |
return inner |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment