+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+static struct timespec start_time;
+
+void start_benchmark_timer()
+{
+ if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time) == -1) {
+ perror("clock_gettime()");
+ exit(1);
+ }
+}
+
+static double timediff(const struct timespec* a, const struct timespec* b)
+{
+ return (double)(b->tv_sec - a->tv_sec) +
+ (double)(b->tv_nsec - a->tv_nsec) * 1e-9;
+}
+
+double stop_benchmark_timer()
+{
+ struct timespec now;
+ if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &now) == -1) {
+ perror("clock_gettime()");
+ exit(1);
+ }
+
+ return timediff(&start_time, &now);
+}