Skip to content

Instantly share code, notes, and snippets.

@jaxFF
Created December 12, 2022 21:27
Show Gist options
  • Save jaxFF/3e7f6b6623948fcceee58329623e4609 to your computer and use it in GitHub Desktop.
Save jaxFF/3e7f6b6623948fcceee58329623e4609 to your computer and use it in GitHub Desktop.
## POSIX Specific Timer:
#ifndef POSIX_SPECIFIC_H
#define POSIX_SPECIFIC_H
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
global b32 posix_global_init = false;
global struct timespec posix_global_res = {0};
global struct timespec posix_global_start_time = {0};
// NOTE: 1e9 is how many nanoseconds per second
global u64 os_ticks_per_second() {
u64 result = 0;
if (!posix_global_init) {
clock_getres(CLOCK_MONOTONIC_RAW, &posix_global_res);
clock_gettime(CLOCK_MONOTONIC_RAW, &posix_global_start_time);
posix_global_init = true;
}
result = (u64)((posix_global_res.tv_sec) + (posix_global_res.tv_nsec * (u64)1e9));
return result;
}
global u64 os_time_elapsed_ticks() {
u64 result = 0;
if (!posix_global_init) {
clock_getres(CLOCK_MONOTONIC_RAW, &posix_global_res);
clock_gettime(CLOCK_MONOTONIC_RAW, &posix_global_start_time);
posix_global_init = true;
}
struct timespec time_now = {0};
clock_gettime(CLOCK_MONOTONIC, &time_now);;
result = (u64)(((time_now.tv_sec - posix_global_start_time.tv_sec * (u64)1e9)) +
((time_now.tv_nsec - posix_global_start_time.tv_nsec / 1000ULL)));
#if 0
printf("Pure now: %0.32f s ", (f64)(time_now.tv_sec - posix_global_start_time.tv_sec));
printf("%0.8f ns ", (f64)(time_now.tv_nsec - posix_global_start_time.tv_nsec));
printf("%0.32f combined\n", (f64)(time_now.tv_sec - posix_global_start_time.tv_sec) + (f64)(time_now.tv_nsec - posix_global_start_time.tv_nsec));
#endif
return result;
}
#endif
## Win32 specific timer
#ifndef WIN32_SPECIFIC_H
#define WIN32_SPECIFIC_H
#include <timeapi.h>
global LARGE_INTEGER win32_global_freq = {0};
global LARGE_INTEGER win32_global_start_time = {0};
global b32 win32_global_init = false;
b32 RtlQueryPerformanceCounter(LARGE_INTEGER*);
global u64 os_time_elapsed_ticks() {
u64 result = 0;
if (!win32_global_init) {
QueryPerformanceFrequency(&win32_global_freq);
QueryPerformanceCounter(&win32_global_start_time);
timeBeginPeriod(1);
win32_global_init = true;
}
LARGE_INTEGER time_now = {0};
QueryPerformanceCounter(&time_now);
result = ((u64)time_now.HighPart << 32) | time_now.LowPart;
result -= (((u64)win32_global_start_time.HighPart << 32) | win32_global_start_time.LowPart);
return result;
}
global u64 os_ticks_per_second() {
u64 result = 0;
if (!win32_global_init) {
QueryPerformanceFrequency(&win32_global_freq);
QueryPerformanceCounter(&win32_global_start_time);
timeBeginPeriod(1);
win32_global_init = true;
}
result = (((u64)win32_global_freq.HighPart << 32) | win32_global_freq.LowPart);
return result;
}
#endif
## Usage code (I know the conversion is wrong right now):
// I fucking hate dimensional analysis. This is the reciprocal of the tps, producing a timer resolution.
f64 reciprocal_factor = 1.0 / os_ticks_per_second();
f64 ms_per_tick = (1ULL * reciprocal_factor);
f64 ns_per_tick = (1e6 * reciprocal_factor);
f64 us_per_tick = (1000ULL * reciprocal_factor);
f64 s_per_tick = 1.0 / (1000ULL / reciprocal_factor);
u64 start_ticks = os_time_elapsed_ticks();
u64 end_ticks = os_time_elapsed_ticks();
u64 elapsed_ticks = (end_ticks - start_ticks);
f64 elapsed_s = (elapsed_ticks) * s_per_tick;
f64 elapsed_ms = (elapsed_ticks) * ms_per_tick;
f64 elapsed_us = (elapsed_ticks) * us_per_tick;
f64 elapsed_ns = (elapsed_ticks) * ns_per_tick;
printf("%2sElapsed runtime took %0.4fs %0.4fms %0.4fns %0.4f (%0.4fus)\n", " ", elapsed_s, elapsed_ms, elapsed_ns, 1.0 * elapsed_ticks, elapsed_us);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment