From 4880a9d2f46208c70503513961cd36262cc187b4 Mon Sep 17 00:00:00 2001 From: Simon Wiesend Date: Sun, 30 Nov 2025 10:34:01 +0100 Subject: [PATCH] fix: timers on Linux On Linux the clock() function measures cpu time instead of wall time. This change uses the Apple code path for Linux. --- timer.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/timer.c b/timer.c index fd8f6c1..2c80f33 100644 --- a/timer.c +++ b/timer.c @@ -1,6 +1,12 @@ #include "timer.h" -#if __APPLE__ +#ifdef __linux__ +// Defines strict posix compliance for CLOCK_MONOTONIC +#define _POSIX_C_SOURCE 199309L +#include +#endif + +#if __APPLE__ || __linux__ #include static struct timespec start = {0, 0}; @@ -14,14 +20,15 @@ void startTimer() double stopTimer() { struct timespec end; - + clock_gettime(CLOCK_MONOTONIC, &end); unsigned long long delta_us = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000; double measuredSeconds = (double)delta_us / 1000000.; - if(start.tv_nsec > 0) { + if (start.tv_nsec > 0) + { start.tv_nsec = 0; start.tv_sec = 0; } @@ -45,7 +52,7 @@ double stopTimer() { double measuredSeconds = (clock() - (double)startClocks) / CLOCKS_PER_SEC; - if(startClocks > 0) + if (startClocks > 0) startClocks = 0; else measuredSeconds = -1;