commit 5fb61e827a3757d805b3e2e033cd6fa994f079e1 Author: Vitaliy Filippov Date: Tue Sep 28 23:03:44 2021 +0300 clock_gettime test diff --git a/test_clock.c b/test_clock.c new file mode 100644 index 0000000..ca7a952 --- /dev/null +++ b/test_clock.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include "x86intrin.h" + +void test_clock(const char *name, clockid_t clockid) +{ + struct timespec tp, te; + clock_gettime(clockid, &te); + for (int i = 0; i < 100000000; i++) + { + clock_gettime(clockid, &tp); + } + printf("%s: %lu ns\n", name, (tp.tv_sec-te.tv_sec)*10 + (tp.tv_nsec-te.tv_nsec)/100000000); +} + +void test_rdtsc() +{ + struct timespec ts, te; + clock_gettime(CLOCK_MONOTONIC, &ts); + uint64_t s = __rdtsc(), e; + for (int i = 0; i < 100000000; i++) + { + e = __rdtsc(); + } + clock_gettime(CLOCK_MONOTONIC, &te); + printf("RDTSC: %lu ns\n", (te.tv_sec-ts.tv_sec)*10 + (te.tv_nsec-ts.tv_nsec)/100000000); +} + +int main(int narg, char **args) +{ + test_rdtsc(); + test_clock("CLOCK_MONOTONIC_RAW", CLOCK_MONOTONIC_RAW); + test_clock("CLOCK_MONOTONIC", CLOCK_MONOTONIC); + test_clock("CLOCK_MONOTONIC_COARSE", CLOCK_MONOTONIC_COARSE); + test_clock("CLOCK_REALTIME", CLOCK_REALTIME); + test_clock("CLOCK_REALTIME_COARSE", CLOCK_REALTIME_COARSE); + return 0; +}