]> git.sesse.net Git - fjl/blobdiff - benchmark.c
Add the missing benchmarking files.
[fjl] / benchmark.c
diff --git a/benchmark.c b/benchmark.c
new file mode 100644 (file)
index 0000000..8e773e5
--- /dev/null
@@ -0,0 +1,30 @@
+#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);
+}