Skip to content

Instantly share code, notes, and snippets.

@drwilco
Last active December 22, 2015 08:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drwilco/5338577 to your computer and use it in GitHub Desktop.
Save drwilco/5338577 to your computer and use it in GitHub Desktop.
/* gcc -o timebench timebench.c -lrt -O2 -fno-inline */
/* no-inline for better readable objdump -d output */
#include <time.h>
#include <sys/time.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define REPS 10000000
#define VARS \
int i; \
double duration; \
struct timespec start, end;
#define CALC \
duration = end.tv_sec - start.tv_sec; \
duration += ((float) end.tv_nsec) / 1000000000.0; \
duration -= ((float) start.tv_nsec) / 1000000000.0; \
duration *= 1000000000 / REPS; \
static void
bench_cgt(clockid_t clock, char *name)
{
VARS
struct timespec ts;
memset(&ts, 0, sizeof(ts));
clock_gettime(CLOCK_REALTIME, &start);
for (i = 0; i < REPS; i++) {
clock_gettime(clock, &ts);
}
clock_gettime(CLOCK_REALTIME, &end);
CALC
printf("clock_gettime(%s) execution time: %f ns\n", name, duration);
}
static void
bench_cgt_syscall(clockid_t clock, char *name)
{
VARS
struct timespec ts;
memset(&ts, 0, sizeof(ts));
clock_gettime(CLOCK_REALTIME, &start);
for (i = 0; i < REPS; i++) {
syscall(SYS_clock_gettime, clock, &ts);
}
clock_gettime(CLOCK_REALTIME, &end);
CALC
printf("clock_gettime(%s) syscall execution time: %f ns\n", name, duration);
}
static void
bench_time(void)
{
VARS
clock_gettime(CLOCK_REALTIME, &start);
for (i = 0; i < REPS; i++) {
time(NULL);
}
clock_gettime(CLOCK_REALTIME, &end);
CALC
printf("time() execution time: %f ns\n", duration);
}
static void
bench_gtod(void)
{
VARS
struct timeval tv;
clock_gettime(CLOCK_REALTIME, &start);
for (i = 0; i < REPS; i++) {
gettimeofday(&tv, NULL);
}
clock_gettime(CLOCK_REALTIME, &end);
CALC
printf("gettimeofday() execution time: %f ns\n", duration);
}
volatile struct timespec vts;
static void
bench_v(void)
{
VARS
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &vts);
clock_gettime(CLOCK_REALTIME, &start);
for (i = 0; i < REPS; i++) {
ts = vts;
}
clock_gettime(CLOCK_REALTIME, &end);
CALC
printf("volatile timespec execution time: %f ns\n", duration);
}
static void
bench_select(void)
{
VARS
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
clock_gettime(CLOCK_REALTIME, &start);
for (i = 0; i < REPS; i++) {
select(0, NULL, NULL, NULL, &tv);
}
clock_gettime(CLOCK_REALTIME, &end);
CALC
printf("select() execution time: %f ns\n", duration);
}
#define bench(clock) \
bench_cgt(clock, #clock); \
bench_cgt_syscall(clock, #clock)
int
main(void)
{
bench(CLOCK_REALTIME);
bench(CLOCK_MONOTONIC);
#ifdef CLOCK_MONOTONIC_RAW
bench(CLOCK_MONOTONIC_RAW);
#endif
#ifdef CLOCK_REALTIME_COARSE
bench(CLOCK_REALTIME_COARSE);
#endif
#ifdef CLOCK_MONOTONIC_COARSE
bench(CLOCK_MONOTONIC_COARSE);
#endif
bench_time();
bench_gtod();
bench_v();
bench_select();
return(EXIT_SUCCESS);
}
clock_gettime(CLOCK_REALTIME) execution time: 21.616938 ns
clock_gettime(CLOCK_REALTIME) syscall execution time: 73.070474 ns
clock_gettime(CLOCK_MONOTONIC) execution time: 21.123462 ns
clock_gettime(CLOCK_MONOTONIC) syscall execution time: 74.661213 ns
clock_gettime(CLOCK_MONOTONIC_RAW) execution time: 76.850437 ns
clock_gettime(CLOCK_MONOTONIC_RAW) syscall execution time: 73.835467 ns
clock_gettime(CLOCK_REALTIME_COARSE) execution time: 7.176166 ns
clock_gettime(CLOCK_REALTIME_COARSE) syscall execution time: 64.750906 ns
clock_gettime(CLOCK_MONOTONIC_COARSE) execution time: 7.935776 ns
clock_gettime(CLOCK_MONOTONIC_COARSE) syscall execution time: 66.636675 ns
time() execution time: 3.947946 ns
gettimeofday() execution time: 21.515203 ns
volatile timespec execution time: 0.721741 ns
select() execution time: 221.470701 ns
clock_gettime(CLOCK_REALTIME) execution time: 5523.867456 ns
clock_gettime(CLOCK_REALTIME) syscall execution time: 5184.366656 ns
clock_gettime(CLOCK_MONOTONIC) execution time: 5528.299328 ns
clock_gettime(CLOCK_MONOTONIC) syscall execution time: 5438.887168 ns
clock_gettime(CLOCK_MONOTONIC_RAW) execution time: 5501.085728 ns
clock_gettime(CLOCK_MONOTONIC_RAW) syscall execution time: 5281.261408 ns
clock_gettime(CLOCK_REALTIME_COARSE) execution time: 9.114048 ns
clock_gettime(CLOCK_REALTIME_COARSE) syscall execution time: 99.776320 ns
clock_gettime(CLOCK_MONOTONIC_COARSE) execution time: 9.789248 ns
clock_gettime(CLOCK_MONOTONIC_COARSE) syscall execution time: 104.612224 ns
time() execution time: 4.316992 ns
gettimeofday() execution time: 5285.404384 ns
volatile timespec execution time: 1.489872 ns
select() execution time: 367.661456 ns
clock_gettime(CLOCK_REALTIME) execution time: 205.713536 ns
clock_gettime(CLOCK_REALTIME) syscall execution time: 203.797728 ns
clock_gettime(CLOCK_MONOTONIC) execution time: 216.525120 ns
clock_gettime(CLOCK_MONOTONIC) syscall execution time: 209.305139 ns
clock_gettime(CLOCK_MONOTONIC_RAW) execution time: 208.499407 ns
clock_gettime(CLOCK_MONOTONIC_RAW) syscall execution time: 203.348128 ns
clock_gettime(CLOCK_REALTIME_COARSE) execution time: 8.472448 ns
clock_gettime(CLOCK_REALTIME_COARSE) syscall execution time: 131.531040 ns
clock_gettime(CLOCK_MONOTONIC_COARSE) execution time: 8.876096 ns
clock_gettime(CLOCK_MONOTONIC_COARSE) syscall execution time: 135.566848 ns
time() execution time: 4.093056 ns
gettimeofday() execution time: 201.200128 ns
volatile timespec execution time: 0.871808 ns
select() execution time: 292.280128 ns
clock_gettime(CLOCK_REALTIME) execution time: 133.198007 ns
clock_gettime(CLOCK_REALTIME) syscall execution time: 606.988477 ns
clock_gettime(CLOCK_MONOTONIC) execution time: 112.581098 ns
clock_gettime(CLOCK_MONOTONIC) syscall execution time: 585.660957 ns
time() execution time: 124.435171 ns
gettimeofday() execution time: 113.365549 ns
volatile timespec execution time: 5.153952 ns
select() execution time: 748.764848 ns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment