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