Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #
- # Python Timer Class - Context Manager for Timing Code Blocks
- # Corey Goldberg - 2012
- #
- from timeit import default_timer
- class Timer(object):
- def __init__(self, verbose=False):
- self.verbose = verbose
- self.timer = default_timer
- def __enter__(self):
- self.start = self.timer()
- return self
- def __exit__(self, *args):
- end = self.timer()
- self.elapsed_secs = end - self.start
- self.elapsed = self.elapsed_secs * 1000 # millisecs
- if self.verbose:
- print('elapsed time: {:.4f} ms'.format(self.elapsed))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement