static double run_bench( struct ilka_bench *bench, ilka_bench_fn_t fn, void *ctx, size_t id, size_t n) { fn(bench, ctx, id, n); if (!bench->stopped) ilka_bench_stop(bench); ilka_assert(bench->started, "bench_start was not called"); return bench_elapsed(bench); }
void bench_start(struct bench *bench) { struct timeval val; (void) gettimeofday(&val, NULL); bench->start = (struct bench_time) { .seconds = val.tv_sec, .microseconds = val.tv_usec, }; } void bench_stop(struct bench *bench) { struct timeval val; (void) gettimeofday(&val, NULL); bench->stop = (struct bench_time) { .seconds = val.tv_sec, .microseconds = val.tv_usec, }; } void bench_elapsed(const struct bench *bench, struct bench_time *result) { result->seconds = bench->stop.seconds - bench->start.seconds; result->microseconds = bench->stop.microseconds - bench->start.microseconds; if (result->microseconds < 0) { result->microseconds += 1000000; result->seconds--; } } char * bench_elapsed_str(const struct bench *bench) { struct bench_time elapsed; char *buf; int ret; bench_elapsed(bench, &elapsed); ret = asprintf(&buf, "%ld.%06ld", elapsed.seconds, elapsed.microseconds); assert(ret >= 0); return buf; }