27 lines
732 B
Python
27 lines
732 B
Python
# ############################
|
|
# from github gist: nealtodd/decorator.py (https://gist.github.com/nealtodd/2489618)
|
|
# ############################
|
|
|
|
import pstats
|
|
from cProfile import Profile
|
|
|
|
|
|
def profile(sort_args=None, print_args=None):
|
|
sort_args = sort_args or ['cumulative']
|
|
print_args = print_args or [20]
|
|
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
|