From: Simon Cross Date: Fri, 9 Sep 2016 18:36:45 +0000 (+0200) Subject: Add utility for detailed performance profiling. X-Git-Tag: tabakrolletjie-v1.0.0~165 X-Git-Url: https://git.ctpug.org.za/?p=tabakrolletjie.git;a=commitdiff_plain;h=fbe1062016a06f39b908f4f050f82ce8fa78008e Add utility for detailed performance profiling. --- diff --git a/tabakrolletjie/utils.py b/tabakrolletjie/utils.py index b605ba1..0775204 100644 --- a/tabakrolletjie/utils.py +++ b/tabakrolletjie/utils.py @@ -21,3 +21,34 @@ def debug_timer(label, debug=False): print "%s [%g seconds]" % (label, duration) return wrapper return debug_inner + + +class DetailedTimer(object): + """ A detailed timer for overly complex functions. + """ + def __init__(self, title, debug=False): + if DEBUG or debug: + self.title = title + self.times = [] + else: + # fast funtions with the correct signature + self.start = int + self.show = str + self.lap = str + self.end = int + + def start(self): + print "---- %s ----" % self.title + self.times.append(time.time()) + + def lap(self, label): + now = time.time() + print " %s: %s" % (label, now - self.times[-1]) + self.times.append(now) + + def show(self, text): + print " %s" % text + + def end(self): + print " %s: %s" % ("total", time.time() - self.times[0]) + print "---- ----"