From 30c47d685dee8628cf45c22dcff0db803566442e Mon Sep 17 00:00:00 2001 From: Ico Doornekamp Date: Sat, 20 May 2023 14:16:36 +0200 Subject: [PATCH] Fixed :cputime because msdn does not implement clock() properly --- src/core/util.c | 10 ++++++---- test/suite0007.janet | 8 ++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/core/util.c b/src/core/util.c index 623ef57e..a699965e 100644 --- a/src/core/util.c +++ b/src/core/util.c @@ -892,11 +892,13 @@ int janet_gettime(struct timespec *spec, enum JanetTimeSource source) { QueryPerformanceCounter(&count); QueryPerformanceFrequency(&perf_freq); spec->tv_sec = count.QuadPart / perf_freq.QuadPart; - spec->tv_nsec = (count.QuadPart % perf_freq.QuadPart) * 1000000000 / perf_freq.QuadPart; + spec->tv_nsec = (long)((count.QuadPart % perf_freq.QuadPart) * 1000000000 / perf_freq.QuadPart); } else if (source == JANET_TIME_CPUTIME) { - float tmp = clock(); - spec->tv_sec = tmp / CLOCKS_PER_SEC; - spec->tv_nsec = (tmp - spec->tv_sec * CLOCKS_PER_SEC) * 1e9 / CLOCKS_PER_SEC; + FILETIME creationTime, exitTime, kernelTime, userTime; + GetProcessTimes(GetCurrentProcess(), &creationTime, &exitTime, &kernelTime, &userTime); + int64_t tmp = ((int64_t)userTime.dwHighDateTime << 32) + userTime.dwLowDateTime; + spec->tv_sec = tmp / 10000000LL; + spec->tv_nsec = tmp % 10000000LL * 100; } return 0; } diff --git a/test/suite0007.janet b/test/suite0007.janet index f8f74bea..e59b049c 100644 --- a/test/suite0007.janet +++ b/test/suite0007.janet @@ -334,16 +334,16 @@ (assert (pos? (length (gensym))) "gensym not empty, regression #753") -# os/clock +# os/clock. These tests might prove fragile under CI because they +# rely on measured time. We'll see. (defmacro measure-time [clocks & body] - (def t1 (gensym)) - (def t2 (gensym)) + (def [t1 t2] [(gensym) (gensym)]) ~(do (def ,t1 (map |(os/clock $) ,clocks)) ,;body (def ,t2 (map |(os/clock $) ,clocks)) - (zipcoll ,clocks [ (- (,t2 0) (,t1 0)) (- (,t2 1) (,t1 1)) (- (,t2 2) (,t1 2))])) + (zipcoll ,clocks (map |(- ;$) (map tuple ,t2 ,t1)))) ) # Spin for 0.1 seconds -- 2.45.2