Skip to content

Instantly share code, notes, and snippets.

@tldrafael
Created February 12, 2020 16:19
Show Gist options
  • Save tldrafael/6ea5d41d6f919c69f7ff6da534906f84 to your computer and use it in GitHub Desktop.
Save tldrafael/6ea5d41d6f919c69f7ff6da534906f84 to your computer and use it in GitHub Desktop.
Decorator to profile python functions
from line_profiler import LineProfiler
def do_profile(follow=[]):
def inner(func):
def profiled_func(*args, **kwargs):
try:
profiler = LineProfiler()
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