Lots of stuff

- Added logging and replaced Timer class with more useful one
- Move projected fit loop out of runtimes
- Eliminate recursions from Fit.clear()
- Clean up overall fit calc logic
This commit is contained in:
blitzmann
2015-07-07 21:48:04 -04:00
committed by blitzmann
parent 23309a5da6
commit c17bce55bb
4 changed files with 124 additions and 78 deletions

View File

@@ -1,30 +1,36 @@
import time
class Timer(object):
"""
Generic timing class for simple profiling.
class Timer():
def __init__(self, name='', logger=None):
self.name = name
self.start = time.time()
self.__last = self.start
self.logger = logger
Usage:
@property
def elapsed(self):
return (time.time() - self.start)*1000
with Timer(verbose=True) as t:
# code to be timed
time.sleep(5)
@property
def last(self):
return (time.time() - self.__last)*1000
Output:
elapsed time: 5000.000 ms
Can also access time with t.secs
"""
def __init__(self, verbose=False):
self.verbose = verbose
def checkpoint(self, name=''):
text = 'Timer - {timer} - {checkpoint} - {last:.2f}ms ({elapsed:.2f}ms elapsed)'.format(
timer=self.name,
checkpoint=name,
last=self.last,
elapsed=self.elapsed
).strip()
self.__last = time.time()
if self.logger:
self.logger.debug(text)
else:
print text
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
def __exit__(self, type, value, traceback):
self.checkpoint('finished')
pass