3 [![build-and-test](https://github.com/google/benchmark/workflows/build-and-test/badge.svg)](https://github.com/google/benchmark/actions?query=workflow%3Abuild-and-test)
4 [![bazel](https://github.com/google/benchmark/actions/workflows/bazel.yml/badge.svg)](https://github.com/google/benchmark/actions/workflows/bazel.yml)
5 [![pylint](https://github.com/google/benchmark/workflows/pylint/badge.svg)](https://github.com/google/benchmark/actions?query=workflow%3Apylint)
6 [![test-bindings](https://github.com/google/benchmark/workflows/test-bindings/badge.svg)](https://github.com/google/benchmark/actions?query=workflow%3Atest-bindings)
7 [![Coverage Status](https://coveralls.io/repos/google/benchmark/badge.svg)](https://coveralls.io/r/google/benchmark)
9 [![Discord](https://discordapp.com/api/guilds/1125694995928719494/widget.png?style=shield)](https://discord.gg/cz7UX7wKC2)
11 A library to benchmark code snippets, similar to unit tests. Example:
14 #include <benchmark/benchmark.h>
16 static void BM_SomeFunction(benchmark::State& state) {
18 for (auto _ : state) {
19 // This code gets timed
23 // Register the function as a benchmark
24 BENCHMARK(BM_SomeFunction);
31 To 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.
35 It may also help to read the [Google Test documentation](https://github.com/google/googletest/blob/main/docs/primer.md)
36 as some of the structural aspects of the APIs are similar.
40 [Discussion group](https://groups.google.com/d/forum/benchmark-discuss)
43 * [libera](https://libera.chat) #benchmark
45 [Additional Tooling Documentation](docs/tools.md)
47 [Assembly Testing Documentation](docs/AssemblyTests.md)
49 [Building and installing Python bindings](docs/python_bindings.md)
53 The library can be used with C++03. However, it requires C++11 to build,
54 including compiler and standard library support.
56 The following minimum versions are required to build the library:
60 * Visual Studio 14 2015
63 See [Platform-Specific Build Instructions](docs/platform_specific_build_instructions.md).
67 This describes the installation process using cmake. As pre-requisites, you'll
68 need git and cmake installed.
70 _See [dependencies.md](docs/dependencies.md) for more details regarding supported
71 versions of build tools._
74 # Check out the library.
75 $ git clone https://github.com/google/benchmark.git
76 # Go to the library root directory
78 # Make a build directory to place the build output.
79 $ cmake -E make_directory "build"
80 # Generate build system files with cmake, and download any dependencies.
81 $ cmake -E chdir "build" cmake -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release ../
82 # or, starting with CMake 3.13, use a simpler form:
83 # cmake -DCMAKE_BUILD_TYPE=Release -S . -B "build"
85 $ cmake --build "build" --config Release
87 This builds the `benchmark` and `benchmark_main` libraries and tests.
88 On a unix system, the build directory should now look something like this:
100 Next, you can run the tests to check the build.
103 $ cmake -E chdir "build" ctest --build-config Release
106 If you want to install the library globally, also run:
109 sudo cmake --build "build" --config Release --target install
112 Note that Google Benchmark requires Google Test to build and run the tests. This
113 dependency can be provided two ways:
115 * Checkout the Google Test sources into `benchmark/googletest`.
116 * Otherwise, if `-DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON` is specified during
117 configuration as above, the library will automatically download and build
118 any required dependencies.
120 If you do not wish to build and run the tests, add `-DBENCHMARK_ENABLE_GTEST_TESTS=OFF`
125 By default, benchmark builds as a debug library. You will see a warning in the
126 output when this is the case. To build it as a release library instead, add
127 `-DCMAKE_BUILD_TYPE=Release` when generating the build system files, as shown
128 above. The use of `--config Release` in build commands is needed to properly
129 support multi-configuration tools (like Visual Studio for example) and can be
130 skipped for other build systems (like Makefile).
132 To enable link-time optimisation, also add `-DBENCHMARK_ENABLE_LTO=true` when
133 generating the build system files.
135 If you are using gcc, you might need to set `GCC_AR` and `GCC_RANLIB` cmake
136 cache variables, if autodetection fails.
138 If you are using clang, you may need to set `LLVMAR_EXECUTABLE`,
139 `LLVMNM_EXECUTABLE` and `LLVMRANLIB_EXECUTABLE` cmake cache variables.
141 To enable sanitizer checks (eg., `asan` and `tsan`), add:
143 -DCMAKE_C_FLAGS="-g -O2 -fno-omit-frame-pointer -fsanitize=address -fsanitize=thread -fno-sanitize-recover=all"
144 -DCMAKE_CXX_FLAGS="-g -O2 -fno-omit-frame-pointer -fsanitize=address -fsanitize=thread -fno-sanitize-recover=all "
147 ### Stable and Experimental Library Versions
149 The main branch contains the latest stable version of the benchmarking library;
150 the API of which can be considered largely stable, with source breaking changes
151 being made only upon the release of a new major version.
153 Newer, experimental, features are implemented and tested on the
154 [`v2` branch](https://github.com/google/benchmark/tree/v2). Users who wish
155 to use, test, and provide feedback on the new features are encouraged to try
156 this branch. However, this branch provides no stability guarantees and reserves
157 the right to change and break the API at any time.
163 Define a function that executes the code to measure, register it as a benchmark
164 function using the `BENCHMARK` macro, and ensure an appropriate `main` function
168 #include <benchmark/benchmark.h>
170 static void BM_StringCreation(benchmark::State& state) {
172 std::string empty_string;
174 // Register the function as a benchmark
175 BENCHMARK(BM_StringCreation);
177 // Define another benchmark
178 static void BM_StringCopy(benchmark::State& state) {
179 std::string x = "hello";
183 BENCHMARK(BM_StringCopy);
188 To run the benchmark, compile and link against the `benchmark` library
189 (libbenchmark.a/.so). If you followed the build steps above, this library will
190 be under the build directory you created.
193 # Example on linux after running the build steps above. Assumes the
194 # `benchmark` and `build` directories are under the current directory.
195 $ g++ mybenchmark.cc -std=c++11 -isystem benchmark/include \
196 -Lbenchmark/build/src -lbenchmark -lpthread -o mybenchmark
199 Alternatively, link against the `benchmark_main` library and remove
200 `BENCHMARK_MAIN();` above to get the same behavior.
202 The compiled executable will run all benchmarks by default. Pass the `--help`
203 flag for option information or see the [User Guide](docs/user_guide.md).
207 If using CMake, it is recommended to link against the project-provided
208 `benchmark::benchmark` and `benchmark::benchmark_main` targets using
209 `target_link_libraries`.
210 It is possible to use ```find_package``` to import an installed version of the
213 find_package(benchmark REQUIRED)
215 Alternatively, ```add_subdirectory``` will incorporate the library directly in
216 to one's CMake project.
218 add_subdirectory(benchmark)
220 Either way, link to the library as follows.
222 target_link_libraries(MyTarget benchmark::benchmark)