Created
December 4, 2013 15:47
-
-
Save durden/7789819 to your computer and use it in GitHub Desktop.
Clever implementation of context manager for profiling with checkpoint functionality
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 time | |
class timewith(): | |
def __init__(self, name=''): | |
self.name = name | |
self.start = time.time() | |
@property | |
def elapsed(self): | |
return time.time() - self.start | |
def checkpoint(self, name=''): | |
print '{timer} {checkpoint} took {elapsed} seconds'.format( | |
timer=self.name, | |
checkpoint=name, | |
elapsed=self.elapsed, | |
).strip() | |
def __enter__(self): | |
return self | |
def __exit__(self, type, value, traceback): | |
self.checkpoint('finished') | |
pass | |
def get_number(): | |
for x in xrange(5000000): | |
yield x | |
def expensive_function(): | |
for x in get_number(): | |
i = x ^ x ^ x | |
return 'some result!' | |
# prints something like: | |
# fancy thing done with something took 0.582462072372 seconds | |
# fancy thing done with something else took 1.75355315208 seconds | |
# fancy thing finished took 1.7535982132 seconds | |
with timewith('fancy thing') as timer: | |
expensive_function() | |
timer.checkpoint('done with something') | |
expensive_function() | |
expensive_function() | |
timer.checkpoint('done with something else') | |
# or directly | |
timer = timewith('fancy thing') | |
expensive_function() | |
timer.checkpoint('done with something') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment