If we were to test more ways of dealing with our problem at hand, we could look for a way to reuse the benchmark code and just pass it to the function used to perform the lookup. Google Benchmark has a feature that we could use for that. The framework actually lets us pass any arguments we want to the benchmark by adding them as additional parameters to the function signature.
Let's see how a unified signature for our benchmark could look with this feature:
void search_in_sorted_vector(benchmark::State &state, auto finder) { auto haystack = make_sorted_vector<int>(MAX_HAYSTACK_SIZE); for (auto _ : state) { benchmark::DoNotOptimize(finder(haystack, NEEDLE)); } }
You can notice the new finder parameter to the function, which is used in the spot where we previously called either find or lower_bound. We can now make our two microbenchmarks using a different macro than we did last time:
BENCHMARK_CAPTURE(search_in_sorted_vector...