Created
July 12, 2024 16:54
-
-
Save richard-to/a89faf26652ccf9a10933383ef29c5ea to your computer and use it in GitHub Desktop.
Simple timer 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
@contextmanager | |
def timer(name): | |
"""Context manager for timing code blocks with custom name.""" | |
start_time = time.time() | |
yield | |
end_time = time.time() | |
elapsed_time = (end_time - start_time) * 1000 | |
print(f"{name} took {elapsed_time:.2f} ms") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment