Skip to content

Instantly share code, notes, and snippets.

@weirddan455
Created December 31, 2021 16:43
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weirddan455/eb807fa48915652abeca3b6421970ab4 to your computer and use it in GitHub Desktop.
Save weirddan455/eb807fa48915652abeca3b6421970ab4 to your computer and use it in GitHub Desktop.
clock_gettime benchmark
#include <time.h>
#include <stdio.h>
#include <stdint.h>
#define MILLION 1000000
#define BILLION 1000000000
int main(void)
{
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
for (int i = 0; i < MILLION; i++)
{
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
}
int64_t nanoseconds = (end.tv_sec - start.tv_sec) * BILLION;
nanoseconds += (end.tv_nsec - start.tv_nsec);
double seconds = (double)nanoseconds / (double)BILLION;
printf("1 million clock_gettime calls took: %f seconds\n", seconds);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment