benchmark.c

Basic benchmarking library in C
git clone git://git.finwo.net/lib/benchmark.c
Log | Files | Refs | README | LICENSE

commit 86e0e5f3d9ec142d140c6d718564f8d7240a3994
parent 84c52cee593edc914e08f09a5c5ff215a7be0ca3
Author: finwo <finwo@pm.me>
Date:   Sun, 18 Feb 2024 23:15:36 +0100

Untested windows support

Diffstat:
Msrc/benchmark.c | 27+++++++++++++++++++++++++++
1 file changed, 27 insertions(+), 0 deletions(-)

diff --git a/src/benchmark.c b/src/benchmark.c @@ -2,7 +2,34 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> + +#if defined(_WIN32) || defined(_WIN64) +#include <Windows.h> +#include <winsock2.h> // For struct_timeval + +int gettimeofday(struct timeval * tp, struct timezone * tzp) { + // Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's + // This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC) + // until 00:00:00 January 1, 1970 + static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL); + + SYSTEMTIME system_time; + FILETIME file_time; + uint64_t time; + + GetSystemTime( &system_time ); + SystemTimeToFileTime( &system_time, &file_time ); + time = ((uint64_t)file_time.dwLowDateTime ) ; + time += ((uint64_t)file_time.dwHighDateTime) << 32; + + tp->tv_sec = (long) ((time - EPOCH) / 10000000L); + tp->tv_usec = (long) (system_time.wMilliseconds * 1000); + return 0; +} + +#else // _WIN32 || _WIN64 #include <sys/time.h> +#endif #include "benchmark.h"