Skip to content

Instantly share code, notes, and snippets.

@richban
Created July 11, 2023 10:59
Show Gist options
  • Save richban/e2e2ab6327b532fc684b3bd2b6a62e66 to your computer and use it in GitHub Desktop.
Save richban/e2e2ab6327b532fc684b3bd2b6a62e66 to your computer and use it in GitHub Desktop.
LineProfiler decorator
from functools import wraps
from line_profiler import LineProfiler
def profile(follow=[]):
def decorator(func):
@wraps(func)
def profiled_func(*args, **kwargs):
try:
profiler = LineProfiler()
profiler.add_function(func.__wrapped__)
for f in follow:
profiler.add_function(f)
profiler.enable_by_count()
return func(*args, **kwargs)
finally:
profiler.print_stats()
profiler.dump_stats("my_profiling_results.lprof")
return profiled_func
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment