Skip to content

Instantly share code, notes, and snippets.

@richard-to
Created July 12, 2024 16:54
Show Gist options
  • Save richard-to/a89faf26652ccf9a10933383ef29c5ea to your computer and use it in GitHub Desktop.
Save richard-to/a89faf26652ccf9a10933383ef29c5ea to your computer and use it in GitHub Desktop.
Simple timer context manager
@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