from timeit import default_timer as timer class Timer(object): def __init__(self, name='Timer'): self.name = name def __enter__(self): self.start = timer() def __exit__(self, *args): self.end = timer() self.duration = self.end - self.start print('%s:' % self.name, self.duration)