40 lines
1.1 KiB
C
40 lines
1.1 KiB
C
#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;
|
|
}
|