@@@ mostly bench docs
[mLib] / test / example / bench.c
diff --git a/test/example/bench.c b/test/example/bench.c
new file mode 100644 (file)
index 0000000..144cf77
--- /dev/null
@@ -0,0 +1,91 @@
+/* -*-c-*-
+ *
+ * Demonstration of standalone benchmarking
+ *
+ * (c) 2024 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of the mLib utilities library.
+ *
+ * mLib is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU Library General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * mLib is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with mLib.  If not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ * USA.
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "macros.h"
+#include "report.h"
+#include "tvec.h"
+#include "tvec-adhoc.h"
+#include "tvec-bench.h"
+#include "tvec-types.h"
+
+#include "example.h"
+
+/*----- Macro from the manpage --------------------------------------------*/
+
+#define BENCHMARK_DECLS                                                        \
+  struct bench_timing _bmark_t;                                                \
+  int _bmark_rc;                                                       \
+  BENCH_MEASURE_DECLS
+
+#define BENCHMARK_TAG(tag, b, unit, base)                              \
+  MC_BEFORE(tag##__benchmark_before, { fflush(stdout); })              \
+  MC_AFTER(tag##__benchmark_after, {                                   \
+    if (_bmark_rc)                                                     \
+      printf(": FAILED\n");                                            \
+    else {                                                             \
+      fputs(": ", stdout);                                             \
+      bench_report(&file_printops, stdout, (unit), &_bmark_t);         \
+      putchar('\n');                                                   \
+    }                                                                  \
+  })                                                                   \
+  BENCH_MEASURE_TAG(tag##__benchmark_measure,                          \
+                   (b), _bmark_rc, &_bmark_t, (base))
+#define BENCHMARK(b, unit, base) BENCHMARK_TAG(bench, b, unit, base)
+
+/*----- Main code ---------------------------------------------------------*/
+
+int main(void)
+{
+  struct bench_state b;
+  BENCHMARK_DECLS;
+
+  if (bench_init(&b, 0))
+    { fprintf(stderr, "timer setup failed\n"); exit(2); }
+  if (bench_calibrate(&b, 0))
+    { fprintf(stderr, "timer calibration failed\n"); exit(2); }
+
+  printf("recfib, n = %u", RECFIBLIMIT);
+  BENCHMARK(&b, BTU_OP, 1)
+    while (_bench_n--) ADMIRE(recfib(RECFIBLIMIT));
+
+  printf("iterfib, n = %u", FIBLIMIT);
+  BENCHMARK(&b, BTU_OP, 1)
+    while (_bench_n--) ADMIRE(iterfib(FIBLIMIT));
+
+  printf("expfib, n = %u", FIBLIMIT);
+  BENCHMARK(&b, BTU_OP, 1)
+    while (_bench_n--) ADMIRE(expfib(FIBLIMIT));
+
+  return (0);
+}
+
+/*----- That's all, folks -------------------------------------------------*/