Generating both the haystack sizes and needle positions might be the easiest when done in a simple function. Google Benchmark allows such scenarios, so let's show how they work in practice.
Let's first rewrite our benchmark function to use two parameters passed in each iteration:
void search_in_sorted_vector(benchmark::State &state, auto finder) { const auto needle = state.range(0); const auto haystack = make_sorted_vector<int>(state.range(1)); for (auto _ : state) { benchmark::DoNotOptimize(finder(haystack, needle)); } }
As you can see, state.range(0) will mark our needle position, while state.range(1) will be the haystack size. This means we need to pass two values each time. Let's create a function that generates them:
void generate_sizes(benchmark::internal::Benchmark *b) { for (long haystack = MIN_HAYSTACK_SIZE; haystack <= MAX_HAYSTACK_SIZE; haystack *= 100) { for (auto needle ...