Last active
February 18, 2020 11:53
-
-
Save kaanberke/47e27bbbd787ee0729305ae9e95e6d3b to your computer and use it in GitHub Desktop.
Timer
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 time import time | |
| class Timer(object): | |
| def __init__(self, logger=None): | |
| self.start = None | |
| self.end = None | |
| self.logger = logger | |
| def __enter__(self): | |
| self.start = time() | |
| def __exit__(self, exc_type, exc_val, exc_tb): | |
| self.end = time() | |
| self.log() | |
| def log(self): | |
| assert self.start is not None and self.end is not None | |
| message = f'Total time: {self.end - self.start}s' | |
| if self.logger is None: | |
| print(message) | |
| else: | |
| self.logger.info(message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.