rcx

library of miscellaneous bits of C code
git clone git://git.rr3.xyz/rcx
Log | Files | Refs | README | LICENSE

commit 1872fdf51e626c60bcc120e1b0979f7fe816d35d
parent bacaaf039091bd29603f4125cbc69c9e834fc71b
Author: robert <robertrussell.72001@gmail.com>
Date:   Wed, 10 Aug 2022 14:41:46 -0700

Namespace all bench functions; use ms for goal time

Using ms for goal time lets use delete SECONDS and MILLISECONDS, thus
simplifying the module.

Diffstat:
Minc/cext/bench.h | 19++++++++-----------
Msrc/bench.c | 11++++++-----
2 files changed, 14 insertions(+), 16 deletions(-)

diff --git a/inc/cext/bench.h b/inc/cext/bench.h @@ -6,23 +6,20 @@ Usage: void my_benchmark(u64 N) { <initialization (not timed)> - bench_start(); + cext_bench_start(); for (u64 i = 0; i < N; i++) { <code to benchmark> } - bench_stop(); + cext_bench_stop(); <cleanup (not timed)> } int main(void) { - bench("my benchmark", my_benchmark, 3*SECONDS); + cext_bench("my benchmark", my_benchmark, 3000); } -Note that <code to benchmark> can contain calls to bench_stop and bench_start -to pause and restart timing. +Note that <code to benchmark> can contain calls to cext_bench_stop and +cext_bench_start to pause and restart timing. */ -#define MILLISECONDS 1000000ULL -#define SECONDS 1000000000ULL - -void bench(char *name, void (*fn)(u64 N), u64 goalns); -void bench_start(void); -void bench_stop(void); +void cext_bench(char *name, void (*fn)(u64 N), u32 goalms); +void cext_bench_start(void); +void cext_bench_stop(void); diff --git a/src/bench.c b/src/bench.c @@ -20,7 +20,7 @@ static u64 run(void (*fn)(u64 N), u64 N); static u64 requiredN(u64 prevN, u64 prevns, u64 goalns); static void printnsop(u64 N, u64 ns); -static bool started; /* Has bench_start been called? */ +static bool started; /* Has cext_bench_start been called? */ static bool active; /* Is the timer currently on? */ static struct timespec start; static u64 accumns; @@ -64,20 +64,21 @@ printnsop(u64 N, u64 ns) { } void -bench(char *name, void (*fn)(u64 N), u64 goalns) { +cext_bench(char *name, void (*fn)(u64 N), u32 goalms) { run(fn, 1); /* Warmup */ + u64 goalns = (u64)goalms * U64_C(1000000); u64 N = 1; u64 ns; while ((ns = run(fn, N)) < goalns && N < MAXN) N = requiredN(N, ns, goalns); - fprintf(stderr, "benchmark: %-25s%10"PRId64" iters ", name, N); + fprintf(stderr, "benchmark: %-25s%10"PRIu64" iters ", name, N); printnsop(N, ns); fprintf(stderr, "\n"); } void -bench_start(void) { +cext_bench_start(void) { if (active) return; active = true; @@ -87,7 +88,7 @@ bench_start(void) { } void -bench_stop(void) { +cext_bench_stop(void) { if (!active) return; struct timespec stop;