-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicro-bench.h
More file actions
390 lines (333 loc) · 10.2 KB
/
micro-bench.h
File metadata and controls
390 lines (333 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//////////////////////////////////////////////////////////////////////
// SPDX-License-Identifier: MIT
//
// micro-bench.h
// =============
//
// Header-only benchmarking library in C99.
//
// Author: Giovanni Santini
// Mail: giovanni.santini@proton.me
// License: MIT
//
//
// Example code
// ------------
//
//
// MicroBench mb = {0}; // define a micro benchmark
//
// for (int i = 0; i < 10; ++i) // Repeat a few times
// {
// micro_bench_start(&mb); // start benchmark
// fib(35); // Do something...
// micro_bench_stop(&mb); // end benchmark
// }
//
// micro_bench_report(&mb); // the default reporter prints to stdout
//
//
// Example output
// --------------
//
//
// |---------------------------------------|
// | Micro benchmark report |
// |---------------------------------------|
// | //// | real | CPU |
// |---------------------------------------|
// | min | 0.0814034 | 0.0810770 |
// | max | 0.0958261 | 0.0954550 |
// | sum | 0.8521938 | 0.8493760 |
// | mean | 0.0852194 | 0.0849376 |
// | var | 0.0000229 | 0.0000226 |
// |---------------------------------------|
// | iterations | 10 |
// |---------------------------------------|
//
//
// Usage
// -----
//
// Do this:
//
// #define MICRO_BENCH_IMPLEMENTATION
//
// before you include this file in *one* C or C++ file to create the
// implementation.
//
// i.e. it should look like this:
//
// #include ...
// #include ...
// #include ...
// #define MICRO_BENCH_IMPLEMENTATION
// #include "micro-bench.h"
//
// Code
// ----
//
// The official git repository of micro-bench.h is hosted at:
//
// https://github.com/San7o/micro-bench.h
//
// This is part of a bigger collection of header-only C99 libraries
// called "micro-headers", contributions are welcome:
//
// https://github.com/San7o/micro-headers
//
#ifndef MICRO_BENCH
#define MICRO_BENCH
#define MICRO_BENCH_MAJOR 0
#define MICRO_BENCH_MINOR 1
#ifdef __cplusplus
extern "C" {
#endif
//
// Configuration
//
// Config: Prefix for all functions
// For function inlining, set this to `static inline` and then define
// the implementation in all the files
#ifndef MICRO_BENCH_DEF
#define MICRO_BENCH_DEF extern
#endif
//
// Types
//
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 199309L
#endif
#ifdef _WIN32
#error "TODO: support for windows clock"
#endif
#include <time.h>
// Data recorded
//
// This is updated each time the start and stop functions are called
typedef struct {
double min_cpu, min_real;
double max_cpu, max_real;
double sum_cpu, sum_real;
double mean_cpu, mean_real;
// running sum of squared deviations from the mean. Used in
// Welford's online algorithm to calculate variance
double M2_cpu, M2_real;
double variance_cpu, variance_real;
long unsigned int iterations;
} MicroBenchData;
// A reporter handles output of data
//
// You can define your own reporter function and use it with
// `micro_bench_report_with`. A few reported are provided by default
//(see below).
typedef void (*MicroBenchReporter)(MicroBenchData *data);
// A micro benchmark
typedef struct {
MicroBenchData data;
clock_t start_time_cpu;
struct timespec start_time_real;
} MicroBench;
//
// Function declarations
//
// Start a benchmark
//
// Time data will be recorded and saved internally.
// Note: you need to call `micro_bench_stop` to end it.
MICRO_BENCH_DEF void micro_bench_start(MicroBench *mb);
// Stop a benchmark
MICRO_BENCH_DEF void micro_bench_stop(MicroBench *mb);
// Reset internal benchmark data captured so far
MICRO_BENCH_DEF void micro_bench_clear(MicroBench *mb);
// Getters for either real time and cpu time
MICRO_BENCH_DEF double micro_bench_get_min_real(MicroBench *mb);
MICRO_BENCH_DEF double micro_bench_get_min_cpu(MicroBench *mb);
MICRO_BENCH_DEF double micro_bench_get_max_real(MicroBench *mb);
MICRO_BENCH_DEF double micro_bench_get_max_cpu(MicroBench *mb);
MICRO_BENCH_DEF double micro_bench_get_mean_real(MicroBench *mb);
MICRO_BENCH_DEF double micro_bench_get_mean_cpu(MicroBench *mb);
MICRO_BENCH_DEF double micro_bench_get_sum_real(MicroBench *mb);
MICRO_BENCH_DEF double micro_bench_get_sum_cpu(MicroBench *mb);
MICRO_BENCH_DEF double micro_bench_get_variance_real(MicroBench *mb);
MICRO_BENCH_DEF double micro_bench_get_variance_cpu(MicroBench *mb);
// Print recorded information to stdout in a nice box
MICRO_BENCH_DEF void
micro_bench_default_reporter_stdout(MicroBenchData *data);
// Print recorded information to stout as CSV
MICRO_BENCH_DEF void
micro_bench_default_reporter_csv(MicroBenchData *data);
// Report benchmark data with the default stdout reporter
MICRO_BENCH_DEF void micro_bench_report(MicroBench *mb);
// Report benchmark data with a specific [reporter]
MICRO_BENCH_DEF void
micro_bench_report_with(MicroBench *mb,
MicroBenchReporter reporter);
//
// Implementation
//
#ifdef MICRO_BENCH_IMPLEMENTATION
#include <stdio.h>
MICRO_BENCH_DEF void micro_bench_start(MicroBench *mb)
{
if (!mb) return;
mb->start_time_cpu = clock();
clock_gettime(CLOCK_MONOTONIC, &mb->start_time_real);
return;
}
MICRO_BENCH_DEF void micro_bench_stop(MicroBench *mb)
{
if (!mb) return;
double stop_time_cpu = clock();
struct timespec stop_time_real;
clock_gettime(CLOCK_MONOTONIC, &stop_time_real);
double diff_cpu = (double)(stop_time_cpu - mb->start_time_cpu) / CLOCKS_PER_SEC;
double diff_real = (stop_time_real.tv_sec - mb->start_time_real.tv_sec)
+ (stop_time_real.tv_nsec - mb->start_time_real.tv_nsec) / 1e9;
if (diff_cpu < mb->data.min_cpu || mb->data.min_cpu == 0.0)
mb->data.min_cpu = diff_cpu;
if (diff_real < mb->data.min_real || mb->data.min_real == 0.0)
mb->data.min_real = diff_real;
if (diff_cpu > mb->data.max_cpu)
mb->data.max_cpu = diff_cpu;
if (diff_real > mb->data.max_real)
mb->data.max_real = diff_real;
mb->data.sum_cpu += diff_cpu;
mb->data.sum_real += diff_real;
mb->data.iterations++;
// Welford's online algorithm to calculate variance
double delta_cpu = diff_cpu - mb->data.mean_cpu;
mb->data.mean_cpu += delta_cpu / mb->data.iterations;
double delta2_cpu = diff_cpu - mb->data.mean_cpu;
mb->data.M2_cpu += delta_cpu * delta2_cpu;
mb->data.variance_cpu = mb->data.M2_cpu / mb->data.iterations;
double delta_real = diff_real - mb->data.mean_real;
mb->data.mean_real += delta_real / mb->data.iterations;
double delta2_real = diff_real - mb->data.mean_real;
mb->data.M2_real += delta_real * delta2_real;
mb->data.variance_real = mb->data.M2_real / mb->data.iterations;
return;
}
MICRO_BENCH_DEF void micro_bench_clear(MicroBench *mb)
{
if (!mb) return;
mb->data = (MicroBenchData){0};
mb->start_time_cpu = (clock_t){0};
mb->start_time_real = (struct timespec){0};
return;
}
MICRO_BENCH_DEF double micro_bench_get_min_real(MicroBench *mb)
{
return mb->data.min_real;
}
MICRO_BENCH_DEF double micro_bench_get_min_cpu(MicroBench *mb)
{
return mb->data.min_cpu;
}
MICRO_BENCH_DEF double micro_bench_get_max_real(MicroBench *mb)
{
return mb->data.max_real;
}
MICRO_BENCH_DEF double micro_bench_get_max_cpu(MicroBench *mb)
{
return mb->data.max_cpu;
}
MICRO_BENCH_DEF double micro_bench_get_mean_real(MicroBench *mb)
{
return mb->data.mean_real;
}
MICRO_BENCH_DEF double micro_bench_get_mean_cpu(MicroBench *mb)
{
return mb->data.mean_cpu;
}
MICRO_BENCH_DEF double micro_bench_get_sum_real(MicroBench *mb)
{
return mb->data.max_real;
}
MICRO_BENCH_DEF double micro_bench_get_sum_cpu(MicroBench *mb)
{
return mb->data.max_cpu;
}
MICRO_BENCH_DEF double micro_bench_get_variance_real(MicroBench *mb)
{
return mb->data.variance_real;
}
MICRO_BENCH_DEF double micro_bench_get_variance_cpu(MicroBench *mb)
{
return mb->data.variance_cpu;
}
MICRO_BENCH_DEF void
micro_bench_default_reporter_stdout(MicroBenchData *data)
{
printf("\n");
printf("/---------------------------------------\\\n");
printf("| Micro benchmark report |\n");
printf("|---------------------------------------|\n");
printf("| //// | real | CPU |\n");
printf("|---------------------------------------|\n");
printf("| min | %1.7f | %1.7f |\n", data->min_real, data->min_cpu);
printf("| max | %1.7f | %1.7f |\n", data->max_real, data->max_cpu);
printf("| sum | %1.7f | %1.7f |\n", data->sum_real, data->sum_cpu);
printf("| mean | %1.7f | %1.7f |\n", data->mean_real, data->mean_cpu);
printf("| var | %1.7f | %1.7f |\n", data->variance_real, data->variance_cpu);
printf("|---------------------------------------|\n");
printf("| iterations | %9ld |\n", data->iterations);
printf("\\---------------------------------------/\n");
return;
}
MICRO_BENCH_DEF void
micro_bench_default_reporter_csv(MicroBenchData *data)
{
printf("min_real,min_cpu,sum_real,sum_cpu,mean_real,mean_cpu,variance_real,variance_cpu,iterations\n");
printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%ld\n",
data->min_real, data->min_cpu, data->max_real, data->max_cpu,
data->sum_real, data->sum_cpu, data->mean_real, data->mean_cpu,
data->variance_real, data->variance_cpu, data->iterations);
return;
}
MICRO_BENCH_DEF void micro_bench_report(MicroBench *mb)
{
micro_bench_report_with(mb, micro_bench_default_reporter_stdout);
return;
}
MICRO_BENCH_DEF void
micro_bench_report_with(MicroBench *mb,
MicroBenchReporter reporter)
{
reporter(&mb->data);
return;
}
#endif // MICRO_BENCH_IMPLEMENTATION
//
// Examples
//
#if 0
#define MICRO_BENCH_IMPLEMENTATION
#include "micro-bench.h"
int fib(int x) {
if (x == 0) return 0;
if (x == 1) return 1;
return fib(x-1) + fib(x-2);
}
int main(void)
{
MicroBench mb;
micro_bench_clear(&mb);
printf("Calculating fibonacci numbers...\n");
for (volatile int i = 0; i < 10; ++i)
{
micro_bench_start(&mb);
fib(35);
micro_bench_stop(&mb);
}
micro_bench_report(&mb);
// or use a specific reporter
printf("\nCSV exporter:\n");
micro_bench_report_with(&mb, micro_bench_default_reporter_csv);
return 0;
}
#endif // 0
#ifdef __cplusplus
}
#endif
#endif // MICRO_BENCH