]> git.sesse.net Git - fjl/commitdiff
Add the missing benchmarking files.
authorSteinar H. Gunderson <sesse@debian.org>
Tue, 20 Jan 2009 23:01:56 +0000 (00:01 +0100)
committerSteinar H. Gunderson <sesse@debian.org>
Tue, 20 Jan 2009 23:01:56 +0000 (00:01 +0100)
benchmark.c [new file with mode: 0644]
benchmark.h [new file with mode: 0644]

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);
+}
diff --git a/benchmark.h b/benchmark.h
new file mode 100644 (file)
index 0000000..2b9ee58
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef _BENCHMARK_H
+#define _BENChMARK_H 1
+
+// Benchmark timing functions for reading out used CPU time. Not thread-safe
+// as they store the information in a global variable.
+void start_benchmark_timer();
+double stop_benchmark_timer();
+
+#endif /* !defined(_BENCHMARK_H) */
+
+