Created
June 18, 2012 14:19
-
-
Save skinp/2948609 to your computer and use it in GitHub Desktop.
Context manager to time python code...
This file contains hidden or 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
from timeit import default_timer | |
class Timer(object): | |
def __init__(self, verbose=False): | |
self.verbose = verbose | |
self.timer = default_timer | |
def __enter__(self): | |
self.start = self.timer() | |
return self | |
def __exit__(self, *args): | |
end = self.timer() | |
self.elapsed_secs = end - self.start | |
self.elapsed = self.elapsed_secs * 1000 # millisecs | |
if self.verbose: | |
print 'elapsed time: %f ms' % self.elapsed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment