Skip to content

Instantly share code, notes, and snippets.

@samueljackson92
Created January 6, 2014 14:02
Show Gist options
  • Save samueljackson92/8283251 to your computer and use it in GitHub Desktop.
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.
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