Skip to content

Instantly share code, notes, and snippets.

@dcragusa
Created September 20, 2018 14:49
Show Gist options
  • Save dcragusa/2cc47d59e68b402b8db591138468066b to your computer and use it in GitHub Desktop.
Save dcragusa/2cc47d59e68b402b8db591138468066b to your computer and use it in GitHub Desktop.
A line by line profiler for Python
# 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