Add timing utility.
authorSimon Cross <hodgestar@gmail.com>
Tue, 6 Sep 2016 18:45:04 +0000 (20:45 +0200)
committerSimon Cross <hodgestar@gmail.com>
Tue, 6 Sep 2016 18:45:04 +0000 (20:45 +0200)
tabakrolletjie/scenes/day.py
tabakrolletjie/utils.py [new file with mode: 0644]

index 9ed67e4ce3206f084316528cabd439f7de2749ee..31590d4bb3558eb3363edc0ea918cb909b0ffcda 100644 (file)
@@ -3,14 +3,12 @@
 import pygame.locals as pgl
 
 import pymunk
-import time
 
 from .base import BaseScene
 from ..lights import BaseLight
 from ..obstacles import BaseObstacle
 from ..events import SceneChangeEvent
-
-from ..constants import DEBUG
+from ..utils import debug_timer
 
 
 class DayScene(BaseScene):
@@ -25,8 +23,8 @@ class DayScene(BaseScene):
         for light in self._lights:
             light.add(self._space)
 
+    @debug_timer("day.render")
     def render(self, surface, gamestate):
-        start_time = time.time()
         surface.fill((0, 0, 155))
         for light in self._lights:
             light.render_light(surface)
@@ -35,10 +33,6 @@ class DayScene(BaseScene):
         for light in self._lights:
             light.render_fittings(surface)
 
-        end_time = time.time()
-        if DEBUG:
-            print "Day Render", end_time - start_time
-
     def event(self, ev, gamestate):
         if ev.type == pgl.KEYDOWN:
             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
@@ -48,8 +42,6 @@ class DayScene(BaseScene):
                 for light in self._lights:
                     light.toggle()
 
+    @debug_timer("day.tick")
     def tick(self, gamestate):
-        start_time = time.time()
-        end_time = time.time()
-        if DEBUG:
-            print "Day Tick", end_time - start_time
+        pass
diff --git a/tabakrolletjie/utils.py b/tabakrolletjie/utils.py
new file mode 100644 (file)
index 0000000..bc5aef2
--- /dev/null
@@ -0,0 +1,23 @@
+""" Tabakutilities. """
+
+import functools
+import time
+
+from .constants import DEBUG
+
+
+def debug_timer(label):
+    """ A decorator for printing how long a function took if debug is true.
+    """
+    def debug_inner(f):
+        @functools.wraps(f)
+        def wrapper(*args, **kw):
+            start_time = time.time()
+            try:
+                return f(*args, **kw)
+            finally:
+                duration = time.time() - start_time
+                if DEBUG:
+                    print "%s [%g seconds]" % (label, duration)
+        return wrapper
+    return debug_inner