From d3f85dcb5cb52ecfa62dc47608238809007df951 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Sat, 21 May 2016 09:57:52 -0400 Subject: [PATCH] lib/realtime: fix clock to return mostly Float values MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/realtime.nit | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/realtime.nit b/lib/realtime.nit index 39c5a27..6e312ef 100644 --- a/lib/realtime.nit +++ b/lib/realtime.nit @@ -121,14 +121,17 @@ end # Keeps track of real time class Clock - # Time at instanciation + + # TODO use less mallocs + + # Time at creation protected var time_at_beginning = new Timespec.monotonic_now # Time at last time a lapse method was called protected var time_at_last_lapse = new Timespec.monotonic_now # Smallest time frame reported by clock - fun resolution : Timespec `{ + fun resolution: Timespec `{ struct timespec* tv = malloc( sizeof(struct timespec) ); #ifdef __MACH__ clock_serv_t cclock; @@ -145,19 +148,27 @@ class Clock return tv; `} - # Return timelapse since instanciation of this instance - fun total : Timespec + # Seconds since the creation of this instance + fun total: Float do - return new Timespec.monotonic_now - time_at_beginning + var now = new Timespec.monotonic_now + var diff = now - time_at_beginning + var r = diff.to_f + diff.free + now.free + return r end - # Return timelapse since last call to lapse - fun lapse : Timespec + # Seconds since the last call to `lapse` + fun lapse: Float do var nt = new Timespec.monotonic_now var dt = nt - time_at_last_lapse + var r = dt.to_f + dt.free + time_at_last_lapse.free time_at_last_lapse = nt - return dt + return r end # Seconds since the last call to `lapse`, without resetting the lapse counter -- 1.7.9.5