From: Simon Cross Date: Tue, 6 Sep 2016 18:45:04 +0000 (+0200) Subject: Add timing utility. X-Git-Tag: tabakrolletjie-v1.0.0~232 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=58ac0aea3fcfbac1a3b2f8ec310c4f5d818bceb4;p=tabakrolletjie.git Add timing utility. --- diff --git a/tabakrolletjie/scenes/day.py b/tabakrolletjie/scenes/day.py index 9ed67e4..31590d4 100644 --- a/tabakrolletjie/scenes/day.py +++ b/tabakrolletjie/scenes/day.py @@ -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 index 0000000..bc5aef2 --- /dev/null +++ b/tabakrolletjie/utils.py @@ -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