View all files | ||||
A library to benchmark code snippets, similar to unit tests. Example:
To get started, see Requirements and Installation. See Usage for a full example and the User Guide for a more comprehensive feature overview.
It may also help to read the Google Test documentation as some of the structural aspects of the APIs are similar.
IRC channels:
Additional Tooling Documentation
Assembly Testing Documentation
Building and installing Python bindings
The library can be used with C++11. However, it requires C++17 to build, including compiler and standard library support.
See dependencies.md for more details regarding supported compilers and standards.
If you have need for a particular compiler to be supported, patches are very welcome.
See Platform-Specific Build Instructions.
This describes the installation process using cmake. As pre-requisites, you'll need git and cmake installed.
See dependencies.md for more details regarding supported versions of build tools.
This builds the benchmark and benchmark_main libraries and tests. On a unix system, the build directory should now look something like this:
Next, you can run the tests to check the build.
If you want to install the library globally, also run:
Note that Google Benchmark requires Google Test to build and run the tests. This dependency can be provided two ways:
If you do not wish to build and run the tests, add -DBENCHMARK_ENABLE_GTEST_TESTS=OFF to CMAKE_ARGS.
By default, benchmark builds as a debug library. You will see a warning in the output when this is the case. To build it as a release library instead, add -DCMAKE_BUILD_TYPE=Release when generating the build system files, as shown above. The use of --config Release in build commands is needed to properly support multi-configuration tools (like Visual Studio for example) and can be skipped for other build systems (like Makefile).
To enable link-time optimisation, also add -DBENCHMARK_ENABLE_LTO=true when generating the build system files.
If you are using gcc, you might need to set GCC_AR and GCC_RANLIB cmake cache variables, if autodetection fails.
If you are using clang, you may need to set LLVMAR_EXECUTABLE, LLVMNM_EXECUTABLE and LLVMRANLIB_EXECUTABLE cmake cache variables.
To enable sanitizer checks (eg., asan and tsan), add:
The main branch contains the latest stable version of the benchmarking library; the API of which can be considered largely stable, with source breaking changes being made only upon the release of a new major version.
Newer, experimental, features are implemented and tested on the v2 branch. Users who wish to use, test, and provide feedback on the new features are encouraged to try this branch. However, this branch provides no stability guarantees and reserves the right to change and break the API at any time.
Define a function that executes the code to measure, register it as a benchmark function using the BENCHMARK macro, and ensure an appropriate main function is available:
To run the benchmark, compile and link against the benchmark library (libbenchmark.a/.so). If you followed the build steps above, this library will be under the build directory you created.
Alternatively, link against the benchmark_main library and remove BENCHMARK_MAIN(); above to get the same behavior.
The compiled executable will run all benchmarks by default. Pass the --help flag for option information or see the User Guide.
If using CMake, it is recommended to link against the project-provided benchmark::benchmark and benchmark::benchmark_main targets using target_link_libraries. It is possible to use find_package to import an installed version of the library.
Alternatively, add_subdirectory will incorporate the library directly in to one's CMake project.
Either way, link to the library as follows.