clock_gettime test

master
Vitaliy Filippov 2021-09-28 23:03:44 +03:00
commit 5fb61e827a
1 changed files with 39 additions and 0 deletions

39
test_clock.c Normal file
View File

@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#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;
}