Created
April 25, 2012 13:15
-
-
Save nealtodd/2489618 to your computer and use it in GitHub Desktop.
Python profiling 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 cProfile import Profile | |
import pstats | |
def profile(sort_args=['cumulative'], print_args=[10]): | |
profiler = Profile() | |
def decorator(fn): | |
def inner(*args, **kwargs): | |
result = None | |
try: | |
result = profiler.runcall(fn, *args, **kwargs) | |
finally: | |
stats = pstats.Stats(profiler) | |
stats.strip_dirs().sort_stats(*sort_args).print_stats(*print_args) | |
return result | |
return inner | |
return 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
""" | |
Python decorator for profiling a function using cProfile. Stats are output to wherever print statements would | |
normally go to, e.g. stdout or a log file. Can be used with Django view functions. | |
pstats' sort and print arguments can be supplied to the decorator. The default is to sort cumulatively and | |
list to first 10 lines. | |
See http://docs.python.org/library/profile.html#instant-user-s-manual for details on pstats sort and print arguments. | |
cProfile is a standard module in most python distributions. pstats is also in many but not all. If pstats doesn't | |
import then install the module via the python-profiler package. E.g. on apt-based systems: | |
sudo apt-get install python-profiler | |
""" | |
# Example without profiling arguments: | |
@profile(): | |
def my_function(my_args): | |
pass | |
# Example with profiling arguments: | |
@profile(sort_args=['cumulative', 'name'], print_args=[.5, 'init']): | |
def my_function(my_args): | |
pass |
Hi @iedmrc, sorry I don't have a more recent version of this.
Thanks for the gist. Works great for me on small profiling tests.
Is there another version that works threadpoolexecutor and/or async functions?
maybe you need some complete profiler that supports threads/async like yappi.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there another version that works threadpoolexecutor and/or async functions?