Created
July 31, 2017 18:37
-
-
Save whalesalad/9958f521fc73b32276b3601ae328e939 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import sys | |
import cProfile | |
import pstats | |
from functools import wraps | |
from contextlib import contextmanager | |
@contextmanager | |
def profile(): | |
pr = cProfile.Profile() | |
pr.enable() | |
yield | |
pr.disable() | |
ps = pstats.Stats(pr, stream=sys.stdout).sort_stats('cumulative') | |
ps.print_stats(20) | |
def with_profiling(fn): | |
""" | |
Use as a decorator to add profiling to a function. | |
""" | |
@wraps(fn) | |
def profiled_function(*args, **kwargs): | |
print('Profiling %s in %s' % (fn.__name__, fn.__module__, )) | |
with profile(): | |
return fn(*args, **kwargs) | |
return profiled_function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment