Fixed test timing on windows
This commit is contained in:
parent
4a5c19a08c
commit
098eabb7e7
1 changed files with 11 additions and 7 deletions
18
src/timer.c
18
src/timer.c
|
@ -1,9 +1,10 @@
|
|||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include "timer.h"
|
||||
|
||||
#define GIGA 1e9
|
||||
#define GIGA 1000000000
|
||||
|
||||
#if defined(__unix__)
|
||||
#if defined(__unix__) && !defined(__CYGWIN__)
|
||||
|
||||
# ifdef CLOCK_MONOTONIC_RAW
|
||||
# define CLOCK CLOCK_MONOTONIC_RAW
|
||||
|
@ -16,14 +17,14 @@ extern __attribute__ ((weak)) int clock_gettime(clockid_t, struct timespec *);
|
|||
#elif defined(__APPLE__)
|
||||
# include <mach/clock.h>
|
||||
# include <mach/mach.h>
|
||||
#elif defined(_WIN32)
|
||||
#elif defined(_WIN32) || defined(__CYGWIN__)
|
||||
# define VC_EXTRALEAN
|
||||
# define WIN32_LEAN_AND_MEAN
|
||||
# include <Windows.h>
|
||||
#endif
|
||||
|
||||
bool can_measure_time(void) {
|
||||
#ifdef __unix__
|
||||
#if defined(__unix__) && !defined(__CYGWIN__)
|
||||
return clock_gettime != NULL;
|
||||
#else
|
||||
return true;
|
||||
|
@ -41,13 +42,16 @@ int gettime_compat(struct timespec_compat *ts) {
|
|||
|
||||
*ts = (struct timespec_compat) { mts->tv_sec, mts->tv_nsec };
|
||||
return res > 0 ? -1 : 0;
|
||||
#elif defined(_WIN32)
|
||||
#elif defined(_WIN32) || defined(__CYGWIN__)
|
||||
LARGE_INTEGER freq, count;
|
||||
if (!QueryPerformanceFrequency(&freq)
|
||||
|| !QueryPerformanceCounter(&count))
|
||||
return -1;
|
||||
|
||||
*ts = (struct timespec_compat) { count / freq, count * (GIGA - 1) / freq };
|
||||
int64_t sec = count.QuadPart / freq.QuadPart;
|
||||
int64_t nano = (int64_t) ((double) count.QuadPart * GIGA / (double) freq.QuadPart) % GIGA;
|
||||
|
||||
*ts = (struct timespec_compat) { sec, nano };
|
||||
return 0;
|
||||
#elif defined(__unix__)
|
||||
if (!can_measure_time()) {
|
||||
|
@ -74,6 +78,6 @@ int timer_end(double *time, struct timespec_compat *state) {
|
|||
if (gettime_compat(&last) == -1)
|
||||
return 0;
|
||||
|
||||
*time = (last.tv_sec - state->tv_sec) + (last.tv_nsec - state->tv_nsec) / GIGA;
|
||||
*time = (last.tv_sec - state->tv_sec) + (last.tv_nsec - state->tv_nsec) / (double) GIGA;
|
||||
return 1;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue