Skip to content

Commit

Permalink
fixup! Benchmark: emulate clock_gettime(2) on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
aklomp committed Jan 9, 2024
1 parent 0a8de55 commit 4944c39
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions test/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <stdio.h>
#include <time.h>

#ifdef _WIN32
#if defined(_WIN32) || defined(_WIN64)
# include <windows.h>
# include <wincrypt.h>
#else
Expand Down Expand Up @@ -66,7 +66,7 @@ bytes_to_mb (size_t bytes)
static bool
get_random_data (struct buffers *b, char **errmsg)
{
#ifdef _WIN32
#if defined(_WIN32) || defined(_WIN64)
HCRYPTPROV hProvider = 0;

if (!CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
Expand Down Expand Up @@ -130,18 +130,30 @@ timediff_sec (base64_timespec *start, base64_timespec *end)

return (float)((diff * tb.numer) / tb.denom) / 1e9f;
}
#elif defined(_WIN32) && !(defined(__MINGW32__) || defined(__MINGW64__))
typedef struct timespec base64_timespec;
#elif defined(_WIN32) || defined(_WIN64)
typedef ULARGE_INTEGER base64_timespec;

static void
base64_gettime (base64_timespec *o_time)
{
timespec_get(o_time, TIME_UTC);
FILETIME current_time_ft;

GetSystemTimePreciseAsFileTime(&current_time_ft);

o_time->LowPart = current_time_ft.dwLowDateTime;
o_time->HighPart = current_time_ft.dwHighDateTime;
}

static float
timediff_sec (base64_timespec *start, base64_timespec *end)
{
return (end->tv_sec - start->tv_sec) + ((float)(end->tv_nsec - start->tv_nsec)) / 1e9f;
const float ssec = start->QuadPart / 10000000;
const float snsec = start->QuadPart % 10000000;

const float esec = end->QuadPart / 10000000;
const float ensec = end->QuadPart % 10000000;

return (esec - ssec) + (ensec - snsec) / 1e6f;
}
#else
typedef struct timespec base64_timespec;
Expand Down

0 comments on commit 4944c39

Please sign in to comment.