Skip to content

Instantly share code, notes, and snippets.

@durden
Created December 4, 2013 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save durden/7789871 to your computer and use it in GitHub Desktop.
Save durden/7789871 to your computer and use it in GitHub Desktop.
Handy little profile decorator with printing capabilities
# Taken from: https://zapier.com/engineering/profiling-python-boss/
import cProfile
def do_cprofile(func):
def profiled_func(*args, **kwargs):
profile = cProfile.Profile()
try:
profile.enable()
result = func(*args, **kwargs)
profile.disable()
return result
finally:
profile.print_stats()
return profiled_func
def get_number():
for x in xrange(5000000):
yield x
@do_cprofile
def expensive_function():
for x in get_number():
i = x ^ x ^ x
return 'some result!'
# perform profiling
result = expensive_function()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment