Skip to content

Instantly share code, notes, and snippets.

@gpakosz
Created March 7, 2017 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gpakosz/93e079fac2c12edbe8fed7950306be69 to your computer and use it in GitHub Desktop.
Save gpakosz/93e079fac2c12edbe8fed7950306be69 to your computer and use it in GitHub Desktop.
uint64_t getChronometerTime() in ms
#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