Last active
May 6, 2024 12:56
-
-
Save jepler/e37be8fc27d6fb77eb6e9746014db925 to your computer and use it in GitHub Desktop.
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 <time.h> | |
#include <stdio.h> | |
#include <errno.h> | |
#include <string.h> | |
#define N (1000000) | |
void clock_test(clockid_t clock, char *clock_str) { | |
struct timespec ts, t0, t1; | |
int r = clock_gettime(clock, &ts); | |
if (r != 0) { printf("%-21: %s\n", clock_str, strerror(errno)); return; } | |
clock_gettime(CLOCK_MONOTONIC, &t0); | |
for(int i=0; i<N; i++) { | |
clock_gettime(clock, &ts); | |
} | |
clock_gettime(CLOCK_MONOTONIC, &t1); | |
double delta_ns = (t1.tv_sec - t0.tv_sec) * 1e9 + | |
(t1.tv_nsec - t0.tv_nsec); | |
double delta_ns_percall = delta_ns / N; | |
printf("%-21s: %6.1fns percall\n", clock_str, delta_ns_percall); | |
} | |
#define CLOCK_TEST(arg) clock_test(arg, 6 + #arg) | |
int main() { | |
CLOCK_TEST(CLOCK_REALTIME); | |
CLOCK_TEST(CLOCK_REALTIME_ALARM); | |
CLOCK_TEST(CLOCK_REALTIME_COARSE); | |
CLOCK_TEST(CLOCK_TAI); | |
CLOCK_TEST(CLOCK_MONOTONIC); | |
CLOCK_TEST(CLOCK_MONOTONIC_COARSE); | |
CLOCK_TEST(CLOCK_MONOTONIC_RAW); | |
CLOCK_TEST(CLOCK_BOOTTIME); | |
CLOCK_TEST(CLOCK_BOOTTIME_ALARM); | |
CLOCK_TEST(CLOCK_PROCESS_CPUTIME_ID); | |
CLOCK_TEST(CLOCK_THREAD_CPUTIME_ID); | |
CLOCK_TEST(CLOCK_REALTIME); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment