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)
8 [![Build Status](https://travis-ci.org/google/benchmark.svg?branch=master)](https://travis-ci.org/google/benchmark)
9 [![Coverage Status](https://coveralls.io/repos/google/benchmark/badge.svg)](https://coveralls.io/r/google/benchmark)
12 A library to benchmark code snippets, similar to unit tests. Example:
15 #include <benchmark/benchmark.h>
17 static void BM_SomeFunction(benchmark::State& state) {
19 for (auto _ : state) {
20 // This code gets timed
24 // Register the function as a benchmark
25 BENCHMARK(BM_SomeFunction);
32 To get started, see [Requirements](#requirements) and
33 [Installation](#installation). See [Usage](#usage) for a full example and the
34 [User Guide](docs/user_guide.md) for a more comprehensive feature overview.
36 It may also help to read the [Google Test documentation](https://github.com/google/googletest/blob/master/docs/primer.md)
37 as some of the structural aspects of the APIs are similar.
41 [Discussion group](https://groups.google.com/d/forum/benchmark-discuss)
44 * [libera](https://libera.chat) #benchmark
46 [Additional Tooling Documentation](docs/tools.md)
48 [Assembly Testing Documentation](docs/AssemblyTests.md)
52 The library can be used with C++03. However, it requires C++11 to build,
53 including compiler and standard library support.
55 The following minimum versions are required to build the library:
59 * Visual Studio 14 2015
62 See [Platform-Specific Build Instructions](docs/platform_specific_build_instructions.md).
66 This describes the installation process using cmake. As pre-requisites, you'll
67 need git and cmake installed.
69 _See [dependencies.md](docs/dependencies.md) for more details regarding supported
70 versions of build tools._
73 # Check out the library.
74 $ git clone https://github.com/google/benchmark.git
75 # Go to the library root directory
77 # Make a build directory to place the build output.
78 $ cmake -E make_directory "build"
79 # Generate build system files with cmake, and download any dependencies.
80 $ cmake -E chdir "build" cmake -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release ../
81 # or, starting with CMake 3.13, use a simpler form:
82 # cmake -DCMAKE_BUILD_TYPE=Release -S . -B "build"
84 $ cmake --build "build" --config Release
86 This builds the `benchmark` and `benchmark_main` libraries and tests.
87 On a unix system, the build directory should now look something like this:
99 Next, you can run the tests to check the build.
102 $ cmake -E chdir "build" ctest --build-config Release
105 If you want to install the library globally, also run:
108 sudo cmake --build "build" --config Release --target install
111 Note that Google Benchmark requires Google Test to build and run the tests. This
112 dependency can be provided two ways:
114 * Checkout the Google Test sources into `benchmark/googletest`.
115 * Otherwise, if `-DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON` is specified during
116 configuration as above, the library will automatically download and build
117 any required dependencies.
119 If you do not wish to build and run the tests, add `-DBENCHMARK_ENABLE_GTEST_TESTS=OFF`
124 By default, benchmark builds as a debug library. You will see a warning in the
125 output when this is the case. To build it as a release library instead, add
126 `-DCMAKE_BUILD_TYPE=Release` when generating the build system files, as shown
127 above. The use of `--config Release` in build commands is needed to properly
128 support multi-configuration tools (like Visual Studio for example) and can be
129 skipped for other build systems (like Makefile).
131 To enable link-time optimisation, also add `-DBENCHMARK_ENABLE_LTO=true` when
132 generating the build system files.
134 If you are using gcc, you might need to set `GCC_AR` and `GCC_RANLIB` cmake
135 cache variables, if autodetection fails.
137 If you are using clang, you may need to set `LLVMAR_EXECUTABLE`,
138 `LLVMNM_EXECUTABLE` and `LLVMRANLIB_EXECUTABLE` cmake cache variables.
140 ### Stable and Experimental Library Versions
142 The main branch contains the latest stable version of the benchmarking library;
143 the API of which can be considered largely stable, with source breaking changes
144 being made only upon the release of a new major version.
146 Newer, experimental, features are implemented and tested on the
147 [`v2` branch](https://github.com/google/benchmark/tree/v2). Users who wish
148 to use, test, and provide feedback on the new features are encouraged to try
149 this branch. However, this branch provides no stability guarantees and reserves
150 the right to change and break the API at any time.
156 Define a function that executes the code to measure, register it as a benchmark
157 function using the `BENCHMARK` macro, and ensure an appropriate `main` function
161 #include <benchmark/benchmark.h>
163 static void BM_StringCreation(benchmark::State& state) {
165 std::string empty_string;
167 // Register the function as a benchmark
168 BENCHMARK(BM_StringCreation);
170 // Define another benchmark
171 static void BM_StringCopy(benchmark::State& state) {
172 std::string x = "hello";
176 BENCHMARK(BM_StringCopy);
181 To run the benchmark, compile and link against the `benchmark` library
182 (libbenchmark.a/.so). If you followed the build steps above, this library will
183 be under the build directory you created.
186 # Example on linux after running the build steps above. Assumes the
187 # `benchmark` and `build` directories are under the current directory.
188 $ g++ mybenchmark.cc -std=c++11 -isystem benchmark/include \
189 -Lbenchmark/build/src -lbenchmark -lpthread -o mybenchmark
192 Alternatively, link against the `benchmark_main` library and remove
193 `BENCHMARK_MAIN();` above to get the same behavior.
195 The compiled executable will run all benchmarks by default. Pass the `--help`
196 flag for option information or see the [User Guide](docs/user_guide.md).
200 If using CMake, it is recommended to link against the project-provided
201 `benchmark::benchmark` and `benchmark::benchmark_main` targets using
202 `target_link_libraries`.
203 It is possible to use ```find_package``` to import an installed version of the
206 find_package(benchmark REQUIRED)
208 Alternatively, ```add_subdirectory``` will incorporate the library directly in
209 to one's CMake project.
211 add_subdirectory(benchmark)
213 Either way, link to the library as follows.
215 target_link_libraries(MyTarget benchmark::benchmark)