utils.py 497 B

1234567891011121314151617181920212223
  1. from __future__ import unicode_literals
  2. import time
  3. __all__ = (
  4. 'TimeIt',
  5. )
  6. class TimeIt(object):
  7. """
  8. Context manager that times the duration of the code body.
  9. The `duration` attribute will contain the execution time in seconds.
  10. """
  11. def __init__(self):
  12. self.duration = None
  13. def __enter__(self):
  14. self.start = time.time()
  15. return self
  16. def __exit__(self, *args):
  17. self.end = time.time()
  18. self.duration = self.end - self.start