1# Benchmark 2 3[](https://github.com/google/benchmark/actions?query=workflow%3Abuild-and-test) 4[](https://github.com/google/benchmark/actions/workflows/bazel.yml) 5[](https://github.com/google/benchmark/actions?query=workflow%3Apylint) 6[](https://github.com/google/benchmark/actions?query=workflow%3Atest-bindings) 7[](https://coveralls.io/r/google/benchmark) 8 9[](https://discord.gg/cz7UX7wKC2) 10 11A library to benchmark code snippets, similar to unit tests. Example: 12 13```c++ 14#include <benchmark/benchmark.h> 15 16static void BM_SomeFunction(benchmark::State& state) { 17 // Perform setup here 18 for (auto _ : state) { 19 // This code gets timed 20 SomeFunction(); 21 } 22} 23// Register the function as a benchmark 24BENCHMARK(BM_SomeFunction); 25// Run the benchmark 26BENCHMARK_MAIN(); 27``` 28 29## Getting Started 30 31To get started, see [Requirements](#requirements) and 32[Installation](#installation). See [Usage](#usage) for a full example and the 33[User Guide](docs/user_guide.md) for a more comprehensive feature overview. 34 35It may also help to read the [Google Test documentation](https://github.com/google/googletest/blob/main/docs/primer.md) 36as some of the structural aspects of the APIs are similar. 37 38## Resources 39 40[Discussion group](https://groups.google.com/d/forum/benchmark-discuss) 41 42IRC channels: 43* [libera](https://libera.chat) #benchmark 44 45[Additional Tooling Documentation](docs/tools.md) 46 47[Assembly Testing Documentation](docs/AssemblyTests.md) 48 49[Building and installing Python bindings](docs/python_bindings.md) 50 51## Requirements 52 53The library can be used with C++03. However, it requires C++14 to build, 54including compiler and standard library support. 55 56_See [dependencies.md](docs/dependencies.md) for more details regarding supported 57compilers and standards._ 58 59If you have need for a particular compiler to be supported, patches are very welcome. 60 61See [Platform-Specific Build Instructions](docs/platform_specific_build_instructions.md). 62 63## Installation 64 65This describes the installation process using cmake. As pre-requisites, you'll 66need git and cmake installed. 67 68_See [dependencies.md](docs/dependencies.md) for more details regarding supported 69versions of build tools._ 70 71```bash 72# Check out the library. 73$ git clone https://github.com/google/benchmark.git 74# Go to the library root directory 75$ cd benchmark 76# Make a build directory to place the build output. 77$ cmake -E make_directory "build" 78# Generate build system files with cmake, and download any dependencies. 79$ cmake -E chdir "build" cmake -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release ../ 80# or, starting with CMake 3.13, use a simpler form: 81# cmake -DCMAKE_BUILD_TYPE=Release -S . -B "build" 82# Build the library. 83$ cmake --build "build" --config Release 84``` 85This builds the `benchmark` and `benchmark_main` libraries and tests. 86On a unix system, the build directory should now look something like this: 87 88``` 89/benchmark 90 /build 91 /src 92 /libbenchmark.a 93 /libbenchmark_main.a 94 /test 95 ... 96``` 97 98Next, you can run the tests to check the build. 99 100```bash 101$ cmake -E chdir "build" ctest --build-config Release 102``` 103 104If you want to install the library globally, also run: 105 106``` 107sudo cmake --build "build" --config Release --target install 108``` 109 110Note that Google Benchmark requires Google Test to build and run the tests. This 111dependency can be provided two ways: 112 113* Checkout the Google Test sources into `benchmark/googletest`. 114* Otherwise, if `-DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON` is specified during 115 configuration as above, the library will automatically download and build 116 any required dependencies. 117 118If you do not wish to build and run the tests, add `-DBENCHMARK_ENABLE_GTEST_TESTS=OFF` 119to `CMAKE_ARGS`. 120 121### Debug vs Release 122 123By default, benchmark builds as a debug library. You will see a warning in the 124output when this is the case. To build it as a release library instead, add 125`-DCMAKE_BUILD_TYPE=Release` when generating the build system files, as shown 126above. The use of `--config Release` in build commands is needed to properly 127support multi-configuration tools (like Visual Studio for example) and can be 128skipped for other build systems (like Makefile). 129 130To enable link-time optimisation, also add `-DBENCHMARK_ENABLE_LTO=true` when 131generating the build system files. 132 133If you are using gcc, you might need to set `GCC_AR` and `GCC_RANLIB` cmake 134cache variables, if autodetection fails. 135 136If you are using clang, you may need to set `LLVMAR_EXECUTABLE`, 137`LLVMNM_EXECUTABLE` and `LLVMRANLIB_EXECUTABLE` cmake cache variables. 138 139To enable sanitizer checks (eg., `asan` and `tsan`), add: 140``` 141 -DCMAKE_C_FLAGS="-g -O2 -fno-omit-frame-pointer -fsanitize=address -fsanitize=thread -fno-sanitize-recover=all" 142 -DCMAKE_CXX_FLAGS="-g -O2 -fno-omit-frame-pointer -fsanitize=address -fsanitize=thread -fno-sanitize-recover=all " 143``` 144 145### Stable and Experimental Library Versions 146 147The main branch contains the latest stable version of the benchmarking library; 148the API of which can be considered largely stable, with source breaking changes 149being made only upon the release of a new major version. 150 151Newer, experimental, features are implemented and tested on the 152[`v2` branch](https://github.com/google/benchmark/tree/v2). Users who wish 153to use, test, and provide feedback on the new features are encouraged to try 154this branch. However, this branch provides no stability guarantees and reserves 155the right to change and break the API at any time. 156 157## Usage 158 159### Basic usage 160 161Define a function that executes the code to measure, register it as a benchmark 162function using the `BENCHMARK` macro, and ensure an appropriate `main` function 163is available: 164 165```c++ 166#include <benchmark/benchmark.h> 167 168static void BM_StringCreation(benchmark::State& state) { 169 for (auto _ : state) 170 std::string empty_string; 171} 172// Register the function as a benchmark 173BENCHMARK(BM_StringCreation); 174 175// Define another benchmark 176static void BM_StringCopy(benchmark::State& state) { 177 std::string x = "hello"; 178 for (auto _ : state) 179 std::string copy(x); 180} 181BENCHMARK(BM_StringCopy); 182 183BENCHMARK_MAIN(); 184``` 185 186To run the benchmark, compile and link against the `benchmark` library 187(libbenchmark.a/.so). If you followed the build steps above, this library will 188be under the build directory you created. 189 190```bash 191# Example on linux after running the build steps above. Assumes the 192# `benchmark` and `build` directories are under the current directory. 193$ g++ mybenchmark.cc -std=c++11 -isystem benchmark/include \ 194 -Lbenchmark/build/src -lbenchmark -lpthread -o mybenchmark 195``` 196 197Alternatively, link against the `benchmark_main` library and remove 198`BENCHMARK_MAIN();` above to get the same behavior. 199 200The compiled executable will run all benchmarks by default. Pass the `--help` 201flag for option information or see the [User Guide](docs/user_guide.md). 202 203### Usage with CMake 204 205If using CMake, it is recommended to link against the project-provided 206`benchmark::benchmark` and `benchmark::benchmark_main` targets using 207`target_link_libraries`. 208It is possible to use ```find_package``` to import an installed version of the 209library. 210```cmake 211find_package(benchmark REQUIRED) 212``` 213Alternatively, ```add_subdirectory``` will incorporate the library directly in 214to one's CMake project. 215```cmake 216add_subdirectory(benchmark) 217``` 218Either way, link to the library as follows. 219```cmake 220target_link_libraries(MyTarget benchmark::benchmark) 221``` 222