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