Skip to content

Instantly share code, notes, and snippets.

@messefor messefor/timer.py
Created Dec 5, 2017

Embed
What would you like to do?
Timer class to measure elapsed time
import time
import pickle
from collections import defaultdict
class Timer:
def __init__(self):
self.time_pool = defaultdict(list)
self.start_dict = {}
def start(self, key='_default'):
self.start_dict[key] = time.time()
def stop(self, key='_default'):
if key not in self.start_dict.keys():
errmsg = 'Timer has not started yet. key:{}'.format(key)
raise ValueError(errmsg)
elapsed = time.time() - self.start_dict[key]
self.time_pool[key].append(elapsed)
def get_all(self):
return dict(self.time_pool)
def get(self, key='_default'):
return self.time_pool[key]
def save_pickle(self, save_path):
with open(save_path, 'wb') as f:
pickle.dump(dict(self.time_pool), f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.