Last active
December 14, 2015 23:49
-
-
Save raymondbutcher/5168588 to your computer and use it in GitHub Desktop.
A Python context which can be used to easily time blocks of code.
This file contains hidden or 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 sys | |
import time | |
from contextlib import contextmanager | |
@contextmanager | |
def time_elapsed(name=''): | |
""" | |
A context manager for timing blocks of code. | |
From https://gist.github.com/raymondbutcher/5168588 | |
""" | |
start = time.time() | |
yield | |
elapsed = (time.time() - start) * 1000 | |
if name: | |
sys.stderr.write('%s took ' % name) | |
if elapsed < 1: | |
sys.stderr.write('%.4f ms\n' % elapsed) | |
else: | |
sys.stderr.write('%d ms\n' % elapsed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: