Created
January 6, 2014 14:02
-
-
Save samueljackson92/8283251 to your computer and use it in GitHub Desktop.
Simple decorator to wrap a function and print out some basic profiling information.
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
def timed(f): | |
import cProfile, pstats, StringIO | |
def wrap(*args): | |
#start profiling | |
pr = cProfile.Profile() | |
pr.enable() | |
#execute function | |
ret = f(*args) | |
#stop profiling and print results | |
pr.disable() | |
s = StringIO.StringIO() | |
sortby = 'cumulative' | |
ps = pstats.Stats(pr, stream=s).sort_stats(sortby) | |
ps.print_stats() | |
print s.getvalue() | |
return ret | |
return wrap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment