Last active
November 11, 2023 16:25
-
-
Save romech/56e7f4bb18cf93eda9ad04d87e8c34b0 to your computer and use it in GitHub Desktop.
Measure code execution time within a block using a Python 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 perf_counter | |
class catchtime: | |
def __enter__(self): | |
self._time_start = perf_counter() | |
return self | |
def __exit__(self, type, value, traceback): | |
self.elapsed = perf_counter() - self._time_start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example: