Created
July 15, 2014 06:18
-
-
Save wbbradley/ab938411dc5dbb51a77f to your computer and use it in GitHub Desktop.
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
import time | |
class Timer(object): | |
def __init__(self, name): | |
self.name = name | |
def __enter__(self): | |
self.start = time.clock() | |
return self | |
def __exit__(self, *args): | |
self.end = time.clock() | |
self.interval = self.end - self.start | |
print '%s took %.03f sec.' % (self.name, self.interval) | |
def __call__(self, f): | |
def wrapped_f(*args, **kwargs): | |
with self: | |
return f(*args, **kwargs) | |
return wrapped_f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment