Created
December 5, 2017 01:36
-
-
Save messefor/c6bb74a362c0f7634d605d59ca6857c1 to your computer and use it in GitHub Desktop.
Timer class to measure elapsed time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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