Last active
October 18, 2025 11:05
-
-
Save nezvers/4656be1cfdc5b66b957b56a867096577 to your computer and use it in GitHub Desktop.
High precision timer/clock (Windows, Linux)
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
| #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