commit efa5101ffd0cf13d5a86e6def1e16ca4adab1bd5 parent 2823dda5cf87d879d93c5ebf26b146dabeca0d48 Author: bsandro <email@bsandro.tech> Date: Fri, 5 Dec 2025 23:37:49 +0200 cputime implementation is now somewhat portable Diffstat:
| M | cputime.h | | | 11 | ++++------- |
1 file changed, 4 insertions(+), 7 deletions(-)
diff --git a/cputime.h b/cputime.h @@ -1,15 +1,12 @@ #pragma once #include <time.h> -static volatile unsigned long long now = 0; +static volatile double time_started = 0; void __attribute((constructor)) begin() { - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - now = ts.tv_sec + ts.tv_nsec; + time_started = clock(); } void __attribute((destructor)) end() { - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - printf("%f s\n", (double)((ts.tv_sec+ts.tv_nsec)-now)/1e9); + double elapsed = clock() - time_started; + printf("Elapsed: %f s\n", elapsed/CLOCKS_PER_SEC); }