chiark / gitweb /
@@@ mostly bench docs
[mLib] / test / example / bench.c
1 /* -*-c-*-
2  *
3  * Demonstration of standalone benchmarking
4  *
5  * (c) 2024 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
12  * mLib is free software: you can redistribute it and/or modify it under
13  * the terms of the GNU Library General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or (at
15  * your option) any later version.
16  *
17  * mLib is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
20  * License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with mLib.  If not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25  * USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include "macros.h"
34 #include "report.h"
35 #include "tvec.h"
36 #include "tvec-adhoc.h"
37 #include "tvec-bench.h"
38 #include "tvec-types.h"
39
40 #include "example.h"
41
42 /*----- Macro from the manpage --------------------------------------------*/
43
44 #define BENCHMARK_DECLS                                                 \
45   struct bench_timing _bmark_t;                                         \
46   int _bmark_rc;                                                        \
47   BENCH_MEASURE_DECLS
48
49 #define BENCHMARK_TAG(tag, b, unit, base)                               \
50   MC_BEFORE(tag##__benchmark_before, { fflush(stdout); })               \
51   MC_AFTER(tag##__benchmark_after, {                                    \
52     if (_bmark_rc)                                                      \
53       printf(": FAILED\n");                                             \
54     else {                                                              \
55       fputs(": ", stdout);                                              \
56       bench_report(&file_printops, stdout, (unit), &_bmark_t);          \
57       putchar('\n');                                                    \
58     }                                                                   \
59   })                                                                    \
60   BENCH_MEASURE_TAG(tag##__benchmark_measure,                           \
61                     (b), _bmark_rc, &_bmark_t, (base))
62 #define BENCHMARK(b, unit, base) BENCHMARK_TAG(bench, b, unit, base)
63
64 /*----- Main code ---------------------------------------------------------*/
65
66 int main(void)
67 {
68   struct bench_state b;
69   BENCHMARK_DECLS;
70
71   if (bench_init(&b, 0))
72     { fprintf(stderr, "timer setup failed\n"); exit(2); }
73   if (bench_calibrate(&b, 0))
74     { fprintf(stderr, "timer calibration failed\n"); exit(2); }
75
76   printf("recfib, n = %u", RECFIBLIMIT);
77   BENCHMARK(&b, BTU_OP, 1)
78     while (_bench_n--) ADMIRE(recfib(RECFIBLIMIT));
79
80   printf("iterfib, n = %u", FIBLIMIT);
81   BENCHMARK(&b, BTU_OP, 1)
82     while (_bench_n--) ADMIRE(iterfib(FIBLIMIT));
83
84   printf("expfib, n = %u", FIBLIMIT);
85   BENCHMARK(&b, BTU_OP, 1)
86     while (_bench_n--) ADMIRE(expfib(FIBLIMIT));
87
88   return (0);
89 }
90
91 /*----- That's all, folks -------------------------------------------------*/