Advertisement
Guest User

Untitled

a guest
Oct 8th, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # Python Timer Class - Context Manager for Timing Code Blocks
  4. # Corey Goldberg - 2012
  5. #
  6.  
  7.  
  8. from timeit import default_timer
  9.  
  10.  
  11. class Timer(object):
  12. def __init__(self, verbose=False):
  13. self.verbose = verbose
  14. self.timer = default_timer
  15.  
  16. def __enter__(self):
  17. self.start = self.timer()
  18. return self
  19.  
  20. def __exit__(self, *args):
  21. end = self.timer()
  22. self.elapsed_secs = end - self.start
  23. self.elapsed = self.elapsed_secs * 1000 # millisecs
  24. if self.verbose:
  25. print('elapsed time: {:.4f} ms'.format(self.elapsed))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement