Skip to content

Instantly share code, notes, and snippets.

@vstoykov
Last active December 11, 2015 20:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save vstoykov/4654239 to your computer and use it in GitHub Desktop.
Simple class for easy measuring time for code execution
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