-
-
Save hungyiwu/b21b8f676aa9d33b0c7c9daa903a4ca1 to your computer and use it in GitHub Desktop.
Article: How To Create Your Own Timing Context Manager
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
from time import time | |
class Timer(object): | |
def __init__(self, description): | |
self.description = description | |
def __enter__(self): | |
self.start = time() | |
def __exit__(self, type, value, traceback): | |
self.end = time() | |
print(f"{self.description}: {self.end - self.start}") | |
with Timer("List Comprehension Example"): | |
s = [x for x in range(10_000_000)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment