Skip to content

m0luskk/bench.c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bench.c

Simple header-only microbenchnmarking library in c99.

Usage example

When compiling, you need to link -lrt and -lm.

#include "bench.h"

#define BENCHMARK_ARRAY_SIZE 2000
static void some_benchmark_iteration(void* data) {
  qsort(data, BENCHMARK_ARRAY_SIZE, sizeof(int),compare_ints);
}
static void some_benchmark(b_state_t* state) {
  srand(time(NULL));
  int arr[BENCHMARK_ARRAY_SIZE];
  for (size_t i = 0; i < BENCHMARK_ARRAY_SIZE; ++i) {
    arr[i] = rand() % INT_MAX;
  }
  b_state_set_iteration_f(state, some_benchmark_iteration, arr);
  b_state_set_processed_bytes(state, sizeof(int) * BENCHMARK_ARRAY_SIZE);
  b_state_set_processed_items(state, BENCHMARK_ARRAY_SIZE);
  b_state_iterate(state);
}

#define BENCHMARK_ARRAY_SIZE2 (BENCHMARK_ARRAY_SIZE * 2)
static void another_benchmark_iteration(void* data) {
  qsort(data, BENCHMARK_ARRAY_SIZE2, sizeof(int),compare_ints);
}
static void another_benchmark(b_state_t* state) {
  srand(time(NULL));
  int arr[BENCHMARK_ARRAY_SIZE2];
  for (size_t i = 0; i < BENCHMARK_ARRAY_SIZE2; ++i) {
    arr[i] = rand() % INT_MAX;
  }
  b_state_set_iteration_f(state, another_benchmark_iteration, arr);
  b_state_set_min_warmup_time_ms(state, 500);
  b_state_set_timer_type(state, B_TIMER_REAL);
  b_state_iterate(state);
}

B_BENCHMARKS(
  some_benchmark,
  another_benchmark
);

B_MAIN()

Output:

| Benchmark name           | Iterations |     Warmup |                 Time |                  CPU |  Bytes per sec |        Items per sec |
| ------------------------ | ---------- | ---------- | -------------------- | -------------------- | -------------- | -------------------- |
| some_benchmark           |       3870 |          0 |    97.97us +/- 22.4% |    89.53us +/- 19.4% |      77.87mb/s |             20413723 |
| another_benchmark        |       2209 |       2209 |   189.68us +/- 16.3% |      0.00ns +/- 0.0% |        0.00b/s |                    0 |

API

The b_state_t type is represent the internal state of concrete benchmark and using for seting the benchmark options. Pointer of this type is argument of benchmark-functions.

Macros

B_BENCHMARKS

B_BENCHMARKS(...)

Registers passed functions as benchmarks.

B_MAIN

B_MAIN()

Expands to main function for current benchmark's translation unit.

B_DO_NOT_OPTIMIZE

#define B_DO_NOT_OPTIMIZE(ADDR)

Uses the compiler barrier to prevent optimizations specified memory.

Functions

b_state_set_iteration_f

static inline void b_state_set_iteration_f(struct b_state* restrict state, b_iteration_f f, void* restrict data);

The b_state_set_iteration_f function is using to set the benchmark's iteration function

b_state_iterate

static inline void b_state_iterate(struct b_state* state);

The b_state_iterate function starts the benchmark iterations.

Note

If you dont set iterated function via b_state_set_iteration_f benchmark does not start. So this feature is mandatory to set up.

b_state_set_min_warmup_time

static inline void b_state_set_min_warmup_time_ms(struct b_state* state, size_t ms);
static inline void b_state_set_min_warmup_time_s(struct b_state* state, size_t s);

The b_state_set_min_warmup_time functions using to set the benchmark's minimum warming up time.

b_state_set_min_benchmark_time

static inline void b_state_set_min_benchmark_time_ms(struct b_state* state, size_t ms);
static inline void b_state_set_min_benchmark_time_s(struct b_state* state, size_t s);

The b_state_set_min_benchmark_time functions using to set the benchmark's minimum measurement time.

b_state_set_timer_type

static inline void b_state_set_timer_type(struct b_state* state, enum b_timer_type type)

Sets the timer type for benchmark. See also: enum b_timer_type

b_state_set_processed_bytes

static inline void b_state_set_processed_bytes(struct b_state* state, size_t val);

The b_state_set_processed_bytes function using for set the amount of bytes thats processed by each iteration.

b_state_set_processed_items

static inline void b_state_set_processed_items(struct b_state* state, size_t val);

The b_state_set_processed_bytes function using for set the amount of items thats processed by each iteration.

Types

b_state_t

The b_state_t type is represent the internal state of concrete benchmark and using for seting the benchmark options. Pointer of this type is argument of benchmarks.

b_bench_f

Each benchmark, that passed to B_BENCHMARK(), must satisfy void benchmark_name(b_state_t*) signature - b_bench_f type.

b_iteration_f

The benchmark's iteration function.

enum b_timer_type

enum b_timer_type {
  B_TIMER_CPU = 1, // Measure only CPU time
  B_TIMER_REAL = 1 << 1, // Measure only real time
  B_TIMER_CPU_AND_REAL = B_TIMER_REAL | B_TIMER_CPU, // Measure both CPU and real
};