Skip to content

Instantly share code, notes, and snippets.

@nezvers
Last active October 18, 2025 11:05
Show Gist options
  • Select an option

  • Save nezvers/4656be1cfdc5b66b957b56a867096577 to your computer and use it in GitHub Desktop.

Select an option

Save nezvers/4656be1cfdc5b66b957b56a867096577 to your computer and use it in GitHub Desktop.
High precision timer/clock (Windows, Linux)
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <time.h>
#endif
static double timer_start = 0.0;
#ifdef _WIN32
static LARGE_INTEGER freq;
#endif
double TimerGetCurrentTime(void){
#ifdef _WIN32
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
return (double)now.QuadPart / (double)freq.QuadPart;
#else
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return (double)now.tv_sec + (double)now.tv_nsec / 1e9;
#endif
}
void TimerInit(void) {
#ifdef _WIN32
QueryPerformanceFrequency(&freq);
#endif
timer_start = TimerGetCurrentTime();
}
double TimerGetTime(void) {
double current = TimerGetCurrentTime();
return (current - timer_start);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment