Implement a Parameterized Benchmark

Implement a Parameterized Benchmark#

This section shows how to implement a parameterized benchmark using cpp-stat-bench library.

Prerequisites#

Sample Code with Explanation#

Example of a parameterized benchmark.#
#include <stat_bench/benchmark_macros.h>

[[nodiscard]] int fibonacci(int number);

// Define a fixture to define parameters.
class FibonacciFixture : public stat_bench::FixtureBase {
public:
    FibonacciFixture() {
        // Define a parameter.
        add_param<int>("number")
            // Add values to be tested.
            ->add(10)
            ->add(15);
    }
};

// Define a benchmark with a fixture.
// The first argument is the fixture class,
// the second argument is the name of the group of benchmarks,
// and the third argument is the name of the case of the benchmark.
STAT_BENCH_CASE_F(FibonacciFixture, "Fibonacci", "fibonacci") {
    const int number =
        // Get the value of the parameter from the current context object.
        stat_bench::current_invocation_context().get_param<int>("number");
    STAT_BENCH_MEASURE() { return fibonacci(number); };
}

// Define main function. (Required once in an executable.)
STAT_BENCH_MAIN

int fibonacci(int number) {
    if (number < 2) {
        return 1;
    }
    return fibonacci(number - 1) + fibonacci(number - 2);
}

Execution#

You can build the above code and run the resulting executable without arguments to see an output similar to the one below.

Example output to the console.#
cpp-stat-bench 0.21.0

Benchmark start at 2025-02-13T11:46:03.631299+0000

Time resolution: 1.000e-09 sec.

========================================================================================================================
Processing Time
========================================================================================================================

>> Fibonacci
                                                                                Time [us]
                                                            Iterations Samples       Mean Std. Err.       Max Custom Outputs (mean)
------------------------------------------------------------------------------------------------------------------------
fibonacci (number=10)                                                1     100     0.1299    0.0004    0.1610
fibonacci (number=15)                                                1     100     1.2954    0.0142    2.6950

========================================================================================================================
Mean Processing Time
========================================================================================================================

>> Fibonacci
                                                                                Time [us]
                                                            Iterations Samples       Mean Std. Err.       Max Custom Outputs (mean)
------------------------------------------------------------------------------------------------------------------------
fibonacci (number=10)                                           315663      30     0.1153    0.0005    0.1214
fibonacci (number=15)                                            26556      30     1.3075    0.0062    1.4165

Benchmark finished at 2025-02-13T11:46:06.793683+0000

Further Reading#