Add utility for detailed performance profiling.
authorSimon Cross <hodgestar@gmail.com>
Fri, 9 Sep 2016 18:36:45 +0000 (20:36 +0200)
committerSimon Cross <hodgestar@gmail.com>
Fri, 9 Sep 2016 18:36:45 +0000 (20:36 +0200)
tabakrolletjie/utils.py

index b605ba15f8b6287a82be2e3943c9194fb30c8b9f..0775204345e60a94146164feb2966bdecb886642 100644 (file)
@@ -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 "---- ----"