From 8c9a97f4cf9f8d55d48981f3d7170f27ce853ce5 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 14 Jun 2011 16:26:53 -0600 Subject: Add Timers to Python module. --- libdimension-python/dimension.pxd | 16 +++++++++++++++ libdimension-python/dimension.pyx | 43 +++++++++++++++++++++++++++++++++++++++ libdimension/dimension/timer.h | 2 ++ libdimension/timer.c | 5 ++++- 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/libdimension-python/dimension.pxd b/libdimension-python/dimension.pxd index 22e9764..f104e9c 100644 --- a/libdimension-python/dimension.pxd +++ b/libdimension-python/dimension.pxd @@ -63,6 +63,19 @@ cdef extern from "../libdimension/dimension.h": void dmnsn_array_remove(dmnsn_array *array, size_t i) void dmnsn_array_apply(dmnsn_array *array, dmnsn_callback_fn *callback) + ########## + # Timers # + ########## + + ctypedef struct dmnsn_timer: + double real + double user + double system + + dmnsn_timer *dmnsn_new_timer() + void dmnsn_complete_timer(dmnsn_timer *timer) + void dmnsn_delete_timer(dmnsn_timer *timer) + ############ # Geometry # ############ @@ -352,6 +365,9 @@ cdef extern from "../libdimension/dimension.h": double adc_bailout unsigned int nthreads + dmnsn_timer *bounding_timer + dmnsn_timer *render_timer + dmnsn_scene *dmnsn_new_scene() void dmnsn_delete_scene(dmnsn_scene *scene) diff --git a/libdimension-python/dimension.pyx b/libdimension-python/dimension.pyx index 54b7e66..442394b 100644 --- a/libdimension-python/dimension.pyx +++ b/libdimension-python/dimension.pyx @@ -27,6 +27,42 @@ import os def dieOnWarnings(alwaysDie): dmnsn_die_on_warnings(alwaysDie) +########## +# Timers # +########## + +cdef class Timer: + cdef dmnsn_timer *_timer + + def __init__(self): + self._timer = dmnsn_new_timer() + + def __dealloc__(self): + dmnsn_delete_timer(self._timer) + + def complete(self): + dmnsn_complete_timer(self._timer) + + property real: + def __get__(self): + return self._timer.real + property user: + def __get__(self): + return self._timer.user + property system: + def __get__(self): + return self._timer.system + + def __str__(self): + return '%.2fs (user: %.2fs; system: %.2fs)' % \ + (self._timer.real, self._timer.user, self._timer.system) + +cdef _rawTimer(dmnsn_timer *timer): + cdef Timer self = Timer.__new__(Timer) + self._timer = timer + DMNSN_INCREF(self._timer) + return self + ############ # Geometry # ############ @@ -887,6 +923,13 @@ cdef class Scene: def __set__(self, n): self._scene.nthreads = n + property boundingTimer: + def __get__(self): + return _rawTimer(self._scene.bounding_timer) + property renderTimer: + def __get__(self): + return _rawTimer(self._scene.render_timer) + def raytrace(self): # Ensure the default texture is complete cdef Texture default = Texture(Black) diff --git a/libdimension/dimension/timer.h b/libdimension/dimension/timer.h index 0a1fde9..bd72a90 100644 --- a/libdimension/dimension/timer.h +++ b/libdimension/dimension/timer.h @@ -28,6 +28,8 @@ typedef struct dmnsn_timer { double real; /**< Wall-clock time. */ double user; /**< Time spent executing. */ double system; /**< Time spent waiting for the system. */ + + dmnsn_refcount refcount; /**< @internal Reference count. */ } dmnsn_timer; /** A standard format string for timers. */ diff --git a/libdimension/timer.c b/libdimension/timer.c index 0e120ce..ad0fd9b 100644 --- a/libdimension/timer.c +++ b/libdimension/timer.c @@ -30,6 +30,7 @@ dmnsn_new_timer(void) { dmnsn_timer *timer = dmnsn_malloc(sizeof(dmnsn_timer)); dmnsn_get_times(timer); + timer->refcount = 1; return timer; } @@ -46,5 +47,7 @@ dmnsn_complete_timer(dmnsn_timer *timer) void dmnsn_delete_timer(dmnsn_timer *timer) { - dmnsn_free(timer); + if (DMNSN_DECREF(timer)) { + dmnsn_free(timer); + } } -- cgit v1.2.3