Created
January 27, 2022 12:36
-
-
Save otsaw/03f38c2b82eb3209e6a294a79ca8265e to your computer and use it in GitHub Desktop.
Python context manager timer
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
import contextlib | |
import time | |
@contextlib.contextmanager | |
def Timer(): | |
start = time.time() | |
yield lambda: time.time() - start | |
print(f"Elapsed: {time.time() - start:.3f} s") | |
with Timer(): | |
do_something() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment