Skip to content

Instantly share code, notes, and snippets.

@crowsonkb
Created June 22, 2015 16:21
Show Gist options
  • Save crowsonkb/6a9508ad75eaf6d64e47 to your computer and use it in GitHub Desktop.
Save crowsonkb/6a9508ad75eaf6d64e47 to your computer and use it in GitHub Desktop.
Timer context manager
import time
class Timer:
def __init__(self, times):
self.times = times
self.start = None
def __enter__(self):
self.start = time.perf_counter()
return self.start
def __exit__(self, exc_type, exc_val, exc_tb):
finish = time.perf_counter()
self.times.append((self.start, finish))
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment