Last active
December 24, 2015 06:49
-
-
Save kanghyojun/6760006 to your computer and use it in GitHub Desktop.
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
| class Timer(object): | |
| def __init__(self): | |
| self.start = None | |
| self.l = [] | |
| def mark(self, name=''): | |
| from time import time | |
| if name: | |
| print name + ' marked.' | |
| if not self.start: | |
| self.start = time() | |
| else: | |
| t = time() - self.start | |
| diff = 0 | |
| if self.l: | |
| diff = t - self.l[len(self.l) - 1][1] | |
| self.l.append((name , t, diff)) | |
| def clear(self): | |
| self.l = [] | |
| def __repr__(self): | |
| r = "" | |
| for i, x in enumerate(self.l): | |
| if x[0]: | |
| r += "%d mark `%s`, %.2f seconds, %.2fs consumed\n" % (i, x[0], x[1], x[2]) | |
| else: | |
| r += "%d mark, %.2f seconds, %.2fs consumed\n\n" % (i, x[1], x[2]) | |
| if self.l: | |
| avg = sum([x[2] for x in self.l]) / float(len(self.l)) | |
| r += 'run %d in %.2fs, average: %.2fs\n' % (len(self.l), | |
| self.l[len(self.l) - 1][1], | |
| avg) | |
| return r |
Author
kanghyojun
commented
Sep 30, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment