51 lines
1.1 KiB
C
51 lines
1.1 KiB
C
/*
|
|
* Copyright (c) Camden Dixie O'Brien
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
#ifndef BENCHMARKING_H
|
|
#define BENCHMARKING_H
|
|
|
|
#include <time.h>
|
|
|
|
typedef struct {
|
|
int reps;
|
|
double total, median, mean, min, max, stddev;
|
|
} benchmark_summary_t;
|
|
|
|
#define CLOCK_MICROS(c) (1000000 * (double)c / CLOCKS_PER_SEC)
|
|
|
|
#define BENCHMARKING_BEGIN() benchmark_print_header()
|
|
#define BENCHMARKING_END() 0
|
|
|
|
#define START_CLOCK() \
|
|
do { \
|
|
benchmark_start = clock(); \
|
|
} while (0)
|
|
|
|
#define STOP_CLOCK() \
|
|
do { \
|
|
benchmark_end = clock(); \
|
|
} while (0)
|
|
|
|
#define RUN_BENCHMARK(reps, name, fn, ...) \
|
|
do { \
|
|
double res[reps]; \
|
|
for (int i = 0; i < reps; ++i) { \
|
|
fn(__VA_ARGS__); \
|
|
res[i] = CLOCK_MICROS(benchmark_end) \
|
|
- CLOCK_MICROS(benchmark_start); \
|
|
} \
|
|
benchmark_summary_t summary; \
|
|
benchmark_summarise(res, reps, &summary); \
|
|
benchmark_print(name, &summary); \
|
|
} while (0)
|
|
|
|
extern clock_t benchmark_start, benchmark_end;
|
|
|
|
void benchmark_summarise(double *res, int reps, benchmark_summary_t *out);
|
|
void benchmark_print_header(void);
|
|
void benchmark_print(const char *name, const benchmark_summary_t *summary);
|
|
|
|
#endif
|