Created
September 3, 2020 13:07
-
-
Save ground0state/9366b42fe8f5c95e9172c968129f3585 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 | |
import logging | |
from contextlib import contextmanager | |
from typing import Optional | |
@contextmanager | |
def timer(name: str, logger: Optional[logging.Logger] = None): | |
t0 = time.time() | |
msg = f"[{name}] start" | |
if logger is None: | |
print(msg) | |
else: | |
logger.info(msg) | |
yield | |
msg = f"[{name}] done in {time.time() - t0:.2f} s" | |
if logger is None: | |
print(msg) | |
else: | |
logger.info(msg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment