aboutsummaryrefslogtreecommitdiff
path: root/bench/bench.h
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-15 08:40:11 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-15 08:40:11 +0200
commitc4a1c97a09da6d8d88c2ac834bf6234506fc41bc (patch)
tree81ce5675ce886aad0309a41f4d6a696826e0a061 /bench/bench.h
parentb377ba8656bfb807ed8dda6c9f26942b50ce626d (diff)
downloadffwd-c4a1c97a09da6d8d88c2ac834bf6234506fc41bc.tar.gz
ffwd-c4a1c97a09da6d8d88c2ac834bf6234506fc41bc.zip
chore: run clang-format
Diffstat (limited to 'bench/bench.h')
-rw-r--r--bench/bench.h74
1 files changed, 35 insertions, 39 deletions
diff --git a/bench/bench.h b/bench/bench.h
index 9a46c71..6089174 100644
--- a/bench/bench.h
+++ b/bench/bench.h
@@ -30,8 +30,8 @@
#include <unistd.h>
typedef struct {
- long n; /* iterations: the measured loop must run exactly n times */
- double t0_ns; /* set by bench_begin(); end of un-measured setup */
+ long n; /* iterations: the measured loop must run exactly n times */
+ double t0_ns; /* set by bench_begin(); end of un-measured setup */
} bench_state_t;
typedef void (*bench_fn_t)(bench_state_t *);
@@ -45,36 +45,29 @@ typedef struct {
* compiler cannot delete the measured loop. */
static volatile float bench_sink;
-static double bench_now_ns(void)
-{
+static double bench_now_ns(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (double)ts.tv_sec * 1e9 + (double)ts.tv_nsec;
}
-static void bench_begin(bench_state_t *b)
-{
- b->t0_ns = bench_now_ns();
-}
+static void bench_begin(bench_state_t *b) { b->t0_ns = bench_now_ns(); }
/* One timed run of fn at n iterations; returns ns/op. */
-static double bench_run_once(bench_fn_t fn, long n)
-{
- bench_state_t b = { n, 0.0 };
+static double bench_run_once(bench_fn_t fn, long n) {
+ bench_state_t b = {n, 0.0};
b.t0_ns = bench_now_ns();
fn(&b);
double elapsed = bench_now_ns() - b.t0_ns;
return elapsed / (double)n;
}
-static int bench_cmp_double(const void *a, const void *c)
-{
+static int bench_cmp_double(const void *a, const void *c) {
double x = *(const double *)a, y = *(const double *)c;
return (x > y) - (x < y);
}
-static double bench_median(double *v, int n)
-{
+static double bench_median(double *v, int n) {
qsort(v, (size_t)n, sizeof(*v), bench_cmp_double);
return n % 2 ? v[n / 2] : 0.5 * (v[n / 2 - 1] + v[n / 2]);
}
@@ -88,13 +81,13 @@ typedef struct {
const char *meta;
} bench_opts_t;
-static void bench_parse_args(bench_opts_t *o, int argc, char **argv)
-{
+static void bench_parse_args(bench_opts_t *o, int argc, char **argv) {
o->benchtime_ms = 300;
o->count = 10;
o->filter = NULL;
o->json_path = NULL;
- if (!o->meta) o->meta = "";
+ if (!o->meta)
+ o->meta = "";
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--benchtime-ms") && i + 1 < argc)
o->benchtime_ms = atol(argv[++i]);
@@ -105,15 +98,16 @@ static void bench_parse_args(bench_opts_t *o, int argc, char **argv)
else if (!strcmp(argv[i], "--json") && i + 1 < argc)
o->json_path = argv[++i];
}
- if (o->benchtime_ms < 1) o->benchtime_ms = 1;
- if (o->count < 1) o->count = 1;
- if (o->count > 1000) o->count = 1000;
+ if (o->benchtime_ms < 1)
+ o->benchtime_ms = 1;
+ if (o->count < 1)
+ o->count = 1;
+ if (o->count > 1000)
+ o->count = 1000;
}
/* Run all cases (subject to the filter) and report. Returns 0 on success. */
-static int bench_main(const bench_opts_t *o,
- const bench_case_t *cases, int n_cases)
-{
+static int bench_main(const bench_opts_t *o, const bench_case_t *cases, int n_cases) {
FILE *jf = NULL;
if (o->json_path) {
jf = fopen(o->json_path, "w");
@@ -123,14 +117,16 @@ static int bench_main(const bench_opts_t *o,
}
char host[256] = "unknown";
gethostname(host, sizeof(host) - 1);
- fprintf(jf, "{\"host\":\"%s\",\"benchtime_ms\":%ld,\"count\":%d,"
+ fprintf(jf,
+ "{\"host\":\"%s\",\"benchtime_ms\":%ld,\"count\":%d,"
"\"meta\":\"%s\",\"benchmarks\":[",
host, o->benchtime_ms, o->count, o->meta);
}
double *samples = (double *)malloc((size_t)o->count * sizeof(double));
if (!samples) {
- if (jf) fclose(jf);
+ if (jf)
+ fclose(jf);
return 1;
}
@@ -140,7 +136,8 @@ static int bench_main(const bench_opts_t *o,
free(all);
free(iters);
free(samples);
- if (jf) fclose(jf);
+ if (jf)
+ fclose(jf);
return 1;
}
@@ -156,8 +153,10 @@ static int bench_main(const bench_opts_t *o,
while (ns_per_op * (double)n < target_ns) {
double want = target_ns / (ns_per_op > 0.0 ? ns_per_op : 1.0);
long next = (long)(want * 1.2) + 1;
- if (next > n * 100) next = n * 100; /* distrust tiny timings */
- if (next <= n) next = n + 1;
+ if (next > n * 100)
+ next = n * 100; /* distrust tiny timings */
+ if (next <= n)
+ next = n + 1;
n = next;
ns_per_op = bench_run_once(cases[c].fn, n);
}
@@ -174,8 +173,7 @@ static int bench_main(const bench_opts_t *o,
for (int s = 0; s < o->count; s++) {
for (int c = 0; c < n_cases; c++)
if (iters[c])
- all[(size_t)c * o->count + s] =
- bench_run_once(cases[c].fn, iters[c]);
+ all[(size_t)c * o->count + s] = bench_run_once(cases[c].fn, iters[c]);
fprintf(stderr, "pass %d/%d (%.0fs elapsed)\n", s + 1, o->count,
(bench_now_ns() - sample_t0) / 1e9);
}
@@ -184,22 +182,20 @@ static int bench_main(const bench_opts_t *o,
for (int c = 0; c < n_cases; c++) {
if (!iters[c])
continue;
- memcpy(samples, all + (size_t)c * o->count,
- (size_t)o->count * sizeof(double));
+ memcpy(samples, all + (size_t)c * o->count, (size_t)o->count * sizeof(double));
if (jf) {
- fprintf(jf, "%s{\"name\":\"%s\",\"iters\":%ld,\"ns_per_op\":[",
- emitted ? "," : "", cases[c].name, iters[c]);
+ fprintf(jf, "%s{\"name\":\"%s\",\"iters\":%ld,\"ns_per_op\":[", emitted ? "," : "",
+ cases[c].name, iters[c]);
for (int s = 0; s < o->count; s++)
fprintf(jf, "%s%.3f", s ? "," : "", samples[s]);
fprintf(jf, "]}");
}
double med = bench_median(samples, o->count);
- double spread = o->count > 1
- ? 100.0 * (samples[o->count - 1] - samples[0]) / med : 0.0;
- printf("%-44s %10ld iters %14.1f ns/op (spread %.1f%%)\n",
- cases[c].name, iters[c], med, spread);
+ double spread = o->count > 1 ? 100.0 * (samples[o->count - 1] - samples[0]) / med : 0.0;
+ printf("%-44s %10ld iters %14.1f ns/op (spread %.1f%%)\n", cases[c].name, iters[c], med,
+ spread);
fflush(stdout);
emitted++;
}