Created
July 11, 2023 10:59
-
-
Save richban/e2e2ab6327b532fc684b3bd2b6a62e66 to your computer and use it in GitHub Desktop.
LineProfiler decorator
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
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