Created
March 7, 2017 12:55
-
-
Save gpakosz/93e079fac2c12edbe8fed7950306be69 to your computer and use it in GitHub Desktop.
uint64_t getChronometerTime() in ms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef _WIN32 | |
#define WIN32_LEAN_AND_MEAN | |
#include <windows.h> | |
LARGE_INTEGER frequency; // init with QueryPerformanceFrequency(&frequency) e.g. at beginning of main() | |
#endif // or go wild and put that in the constructor of a dedicated static object | |
#ifndef _WIN32 | |
#if defined (CLOCK_MONOTONIC) | |
#include <time.h> | |
#else | |
#include <sys/time.h> | |
#endif | |
#endif | |
static uint64_t getChronometerTime() | |
{ | |
#ifdef _WIN32 | |
LARGE_INTEGER t; | |
QueryPerformanceCounter(&t); | |
return t.QuadPart * 1000 / frequency.QuadPart; | |
#else | |
#if defined (CLOCK_MONOTONIC) | |
struct timespec t; | |
clock_gettime(CLOCK_MONOTONIC, &t); | |
return t.tv_sec * 1000 + round(t.tv_nsec / 1000000.0); | |
#else | |
struct timeval t; | |
gettimeofday(&t, 0); | |
return t.tv_sec * 1000 + round(t.tv_usec / 1000.0); | |
#endif | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment