Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created October 21, 2015 19:04
Show Gist options
  • Save sibelius/3920b3eb5adab482b105 to your computer and use it in GitHub Desktop.
Save sibelius/3920b3eb5adab482b105 to your computer and use it in GitHub Desktop.
Line Profiler decorator, it works on pure Python or Web frameworks like Flask or Django
try:
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
except ImportError:
def do_profile(follow=[]):
def inner(func):
def nothing(*args, **kwargs):
return func(*args, **kwargs)
return nothing
return inner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment