Moved compat.py to new dir utils/ and also added timer class

This commit is contained in:
blitzmann
2014-11-29 20:47:53 -05:00
parent 8351b0fc9d
commit 8df7593223
9 changed files with 37 additions and 7 deletions

30
utils/timer.py Normal file
View File

@@ -0,0 +1,30 @@
import time
class Timer(object):
"""
Generic timing class for simple profiling.
Usage:
with Timer(verbose=True) as t:
# code to be timed
time.sleep(5)
Output:
elapsed time: 5000.000 ms
Can also access time with t.secs
"""
def __init__(self, verbose=False):
self.verbose = verbose
def __enter__(self):
self.start = time.time()
return self
def __exit__(self, *args):
self.end = time.time()
self.secs = self.end - self.start
self.msecs = self.secs * 1000 # millisecs
if self.verbose:
print 'elapsed time: %f ms' % self.msecs