Created
December 3, 2013 07:39
-
-
Save mvmocanu/7765423 to your computer and use it in GitHub Desktop.
Profiling
Details: https://zapier.com/engineering/profiling-python-boss/
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
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() |
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
import time | |
def timefunc(f): | |
def f_timer(*args, **kwargs): | |
start = time.time() | |
result = f(*args, **kwargs) | |
end = time.time() | |
print f.__name__, 'took', end - start, 'time' | |
return result | |
return f_timer | |
def get_number(): | |
for x in xrange(5000000): | |
yield x | |
@timefunc | |
def expensive_function(): | |
for x in get_number(): | |
i = x ^ x ^ x | |
return 'some result!' | |
# prints "expensive_function took 0.72583088875 seconds" | |
result = expensive_function() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment