You can now use CodSpeed to benchmark C++ codebases thanks to our new integration with google/benchmark`.
CodSpeed's C++ support works out of the box with CMake:
# Fetch the CodSpeed compatibility layer
FetchContent_Declare(
google_benchmark
GIT_REPOSITORY https://github.com/CodSpeedHQ/codspeed-cpp
SOURCE_SUBDIR google_benchmark
GIT_TAG main # Or chose a specific version or git ref
)
FetchContent_MakeAvailable(google_benchmark)
# Declare your benchmark executable and its sources here
add_executable(bench_fibo benches/fibo.cpp)
# Link your executable against the `benchmark::benchmark`
target_link_libraries(bench_fibo benchmark::benchmark)
You can then write your benchmarks in C++ using the google/benchmark
API.
Here's an example:
#include <benchmark/benchmark.h>
static void BM_Fib(benchmark::State& state) {
auto fib = [](int n) {
return n < 2 ? n : fib(n - 1) + fib(n - 2);
};
for (auto _ : state)
benchmark::DoNotOptimize(fib(10));
}
BENCHMARK(BM_Fib);
BENCHMARK_MAIN();
This can then run both locally and in CI environments using the CodSpeed Runner. Check out the documentation on integrating the runner in CI environments.
Here are a few example repositories of real-world C++ projects:
Check out our C++ documentation for more details.