X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=tabakrolletjie%2Futils.py;h=0775204345e60a94146164feb2966bdecb886642;hb=9b0872898e6cf9d4e5860de577b8ac9565593b6d;hp=bc5aef2572cf42e3c7ba20146fe2bd80f45974ad;hpb=58ac0aea3fcfbac1a3b2f8ec310c4f5d818bceb4;p=tabakrolletjie.git diff --git a/tabakrolletjie/utils.py b/tabakrolletjie/utils.py index bc5aef2..0775204 100644 --- a/tabakrolletjie/utils.py +++ b/tabakrolletjie/utils.py @@ -6,7 +6,7 @@ import time from .constants import DEBUG -def debug_timer(label): +def debug_timer(label, debug=False): """ A decorator for printing how long a function took if debug is true. """ def debug_inner(f): @@ -17,7 +17,38 @@ def debug_timer(label): return f(*args, **kw) finally: duration = time.time() - start_time - if DEBUG: + if DEBUG or debug: 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 "---- ----"