Created
December 31, 2021 16:43
-
-
Save weirddan455/eb807fa48915652abeca3b6421970ab4 to your computer and use it in GitHub Desktop.
clock_gettime benchmark
This file contains 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 <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