Last active
December 11, 2015 20:18
-
-
Save vstoykov/4654239 to your computer and use it in GitHub Desktop.
Simple class for easy measuring time for code execution
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
from time import time | |
class TimeIt: | |
""" | |
Simple class for easy measuring time for code execution | |
Usage: | |
with TimeIt('Simple description of my code'): | |
do something... | |
and do other thing... | |
etc. | |
""" | |
def __init__(self, name=''): | |
self.name = name | |
def __enter__(self): | |
self.started_at = time() | |
print "%s starts" % (self.name,) | |
return self.started_at | |
def __exit__(self, type, value, traceback): | |
if isinstance(value, Exception): | |
return True | |
executed_for = time() - self.started_at | |
print "%s executed for %s s" % (self.name, executed_for) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment