Created
December 4, 2013 15:49
-
-
Save durden/7789871 to your computer and use it in GitHub Desktop.
Handy little profile decorator with printing capabilities
This file contains 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
# 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