A microbenchmark support library
Go to file
Alexander Popov dfdda57a12
Fix DoNotOptimize() GCC compile error with some types (#1340) (#1424)
Non-const DoNotOptimize() can't compile when used with some types.
Example of code which can't compile:

char buffer3[3] = "";
benchmark::DoNotOptimize(buffer3);

Error message:

error: impossible constraint in 'asm'
  asm volatile("" : "+r"(value) : : "memory");

Introduced in 8545dfb (Fix DoNotOptimize() GCC copy overhead (#1340) (#1410))

The cause is compiler can't work with the +r constraint for types that can't
be placed perfectly in registers. For example, char array[3] can't be perfectly
fit in register on x86_64 so it requires placed in memory but constraint
doesn't allow that.

Solution
- Use +m,r constraint for the small objects so the compiler can decide to use
  register or/and memory
- For the big objects +m constraint is used which allows avoiding extra copy
  bug(see #1340)
- The same approach is used for the const version of DoNotOptimize()
  although the const version works fine with the "r" constraint only.
  Using mixed r,m constraint looks more general solution.

See
- Issue #1340 ([BUG] DoNotOptimize() adds overhead with extra copy of argument(gcc))
- Pull request #1410 (Fix DoNotOptimize() GCC copy overhead (#1340) #1410)
- Commit 8545dfb (Fix DoNotOptimize() GCC copy overhead (#1340) (#1410))
2022-07-04 10:27:05 +01:00
.github add multiple OSes to bazel workflow (#1412) 2022-06-13 17:45:20 +01:00
bindings/python Introduce warmup phase to BenchmarkRunner (#1130) (#1399) 2022-05-23 13:50:17 +01:00
cmake Suppress GoogleTest warnings on windows (MSVC) too. (#1320) 2022-01-13 14:21:43 +00:00
config Revert "Add possibility to ask for libbenchmark version number (#1004) (#1403)" (#1417) 2022-06-20 17:52:03 +01:00
docs Remove redundant formatting tags (#1420) 2022-06-28 10:16:28 +01:00
include/benchmark Fix DoNotOptimize() GCC compile error with some types (#1340) (#1424) 2022-07-04 10:27:05 +01:00
src Revert "Add possibility to ask for libbenchmark version number (#1004) (#1403)" (#1417) 2022-06-20 17:52:03 +01:00
test Fix DoNotOptimize() GCC compile error with some types (#1340) (#1424) 2022-07-04 10:27:05 +01:00
tools Revert "Add possibility to ask for libbenchmark version number (#1004) (#1403)" (#1417) 2022-06-20 17:52:03 +01:00
_config.yml enable markdown rendering on github pages 2021-04-28 16:44:33 +01:00
.clang-format Fix .clang-format 2019-04-08 12:38:11 +03:00
.clang-tidy clang-tidy: readability-redundant and performance (#1298) 2021-12-06 11:18:04 +00:00
.gitignore add .DS_Store to .gitignore 2021-08-17 21:46:11 +01:00
.travis.yml Enable various sanitizer builds in github actions (#1167) 2021-06-03 19:45:02 +01:00
.ycm_extra_conf.py Spelling fixes (#543) 2018-03-06 11:44:25 +00:00
appveyor.yml CMake: use full add_test(NAME <> COMMAND <>) signature (#901) 2019-11-05 22:46:13 +03:00
AUTHORS Allow setting the default time unit globally (#1337) 2022-03-04 11:07:01 +00:00
BUILD.bazel Revert "Add possibility to ask for libbenchmark version number (#1004) (#1403)" (#1417) 2022-06-20 17:52:03 +01:00
CMakeLists.txt Revert "Add possibility to ask for libbenchmark version number (#1004) (#1403)" (#1417) 2022-06-20 17:52:03 +01:00
CONTRIBUTING.md Add information about CLAs. 2014-02-12 18:51:08 -05:00
CONTRIBUTORS Allow setting the default time unit globally (#1337) 2022-03-04 11:07:01 +00:00
LICENSE Update LICENSE file to clearly state which file needs BSD 3 (#1366) 2022-03-14 09:50:46 +00:00
README.md Add installation and build instructions for Python bindings (#1392) 2022-05-10 13:16:00 +01:00
requirements.txt bump numby, as per dependabot (#1336) 2022-01-31 10:28:11 +00:00
setup.py Shut down Bazel gracefully and revert wheel build strategy to job matrix (#1383) 2022-04-08 15:00:46 +01:00
WORKSPACE @platforms is magical; remove it from WORKSPACE. (#1356) 2022-02-24 15:06:45 +00:00

Benchmark

build-and-test bazel pylint test-bindings

Build Status Coverage Status

A library to benchmark code snippets, similar to unit tests. Example:

#include <benchmark/benchmark.h>

static void BM_SomeFunction(benchmark::State& state) {
  // Perform setup here
  for (auto _ : state) {
    // This code gets timed
    SomeFunction();
  }
}
// Register the function as a benchmark
BENCHMARK(BM_SomeFunction);
// Run the benchmark
BENCHMARK_MAIN();

Getting Started

To get started, see Requirements and Installation. See Usage for a full example and the User Guide for a more comprehensive feature overview.

It may also help to read the Google Test documentation as some of the structural aspects of the APIs are similar.

Resources

Discussion group

IRC channels:

Additional Tooling Documentation

Assembly Testing Documentation

Building and installing Python bindings

Requirements

The library can be used with C++03. However, it requires C++11 to build, including compiler and standard library support.

The following minimum versions are required to build the library:

  • GCC 4.8
  • Clang 3.4
  • Visual Studio 14 2015
  • Intel 2015 Update 1

See Platform-Specific Build Instructions.

Installation

This describes the installation process using cmake. As pre-requisites, you'll need git and cmake installed.

See dependencies.md for more details regarding supported versions of build tools.

# Check out the library.
$ git clone https://github.com/google/benchmark.git
# Go to the library root directory
$ cd benchmark
# Make a build directory to place the build output.
$ cmake -E make_directory "build"
# Generate build system files with cmake, and download any dependencies.
$ cmake -E chdir "build" cmake -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release ../
# or, starting with CMake 3.13, use a simpler form:
# cmake -DCMAKE_BUILD_TYPE=Release -S . -B "build"
# Build the library.
$ cmake --build "build" --config Release

This builds the benchmark and benchmark_main libraries and tests. On a unix system, the build directory should now look something like this:

/benchmark
  /build
    /src
      /libbenchmark.a
      /libbenchmark_main.a
    /test
      ...

Next, you can run the tests to check the build.

$ cmake -E chdir "build" ctest --build-config Release

If you want to install the library globally, also run:

sudo cmake --build "build" --config Release --target install

Note that Google Benchmark requires Google Test to build and run the tests. This dependency can be provided two ways:

  • Checkout the Google Test sources into benchmark/googletest.
  • Otherwise, if -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON is specified during configuration as above, the library will automatically download and build any required dependencies.

If you do not wish to build and run the tests, add -DBENCHMARK_ENABLE_GTEST_TESTS=OFF to CMAKE_ARGS.

Debug vs Release

By default, benchmark builds as a debug library. You will see a warning in the output when this is the case. To build it as a release library instead, add -DCMAKE_BUILD_TYPE=Release when generating the build system files, as shown above. The use of --config Release in build commands is needed to properly support multi-configuration tools (like Visual Studio for example) and can be skipped for other build systems (like Makefile).

To enable link-time optimisation, also add -DBENCHMARK_ENABLE_LTO=true when generating the build system files.

If you are using gcc, you might need to set GCC_AR and GCC_RANLIB cmake cache variables, if autodetection fails.

If you are using clang, you may need to set LLVMAR_EXECUTABLE, LLVMNM_EXECUTABLE and LLVMRANLIB_EXECUTABLE cmake cache variables.

Stable and Experimental Library Versions

The main branch contains the latest stable version of the benchmarking library; the API of which can be considered largely stable, with source breaking changes being made only upon the release of a new major version.

Newer, experimental, features are implemented and tested on the v2 branch. Users who wish to use, test, and provide feedback on the new features are encouraged to try this branch. However, this branch provides no stability guarantees and reserves the right to change and break the API at any time.

Usage

Basic usage

Define a function that executes the code to measure, register it as a benchmark function using the BENCHMARK macro, and ensure an appropriate main function is available:

#include <benchmark/benchmark.h>

static void BM_StringCreation(benchmark::State& state) {
  for (auto _ : state)
    std::string empty_string;
}
// Register the function as a benchmark
BENCHMARK(BM_StringCreation);

// Define another benchmark
static void BM_StringCopy(benchmark::State& state) {
  std::string x = "hello";
  for (auto _ : state)
    std::string copy(x);
}
BENCHMARK(BM_StringCopy);

BENCHMARK_MAIN();

To run the benchmark, compile and link against the benchmark library (libbenchmark.a/.so). If you followed the build steps above, this library will be under the build directory you created.

# Example on linux after running the build steps above. Assumes the
# `benchmark` and `build` directories are under the current directory.
$ g++ mybenchmark.cc -std=c++11 -isystem benchmark/include \
  -Lbenchmark/build/src -lbenchmark -lpthread -o mybenchmark

Alternatively, link against the benchmark_main library and remove BENCHMARK_MAIN(); above to get the same behavior.

The compiled executable will run all benchmarks by default. Pass the --help flag for option information or see the User Guide.

Usage with CMake

If using CMake, it is recommended to link against the project-provided benchmark::benchmark and benchmark::benchmark_main targets using target_link_libraries. It is possible to use find_package to import an installed version of the library.

find_package(benchmark REQUIRED)

Alternatively, add_subdirectory will incorporate the library directly in to one's CMake project.

add_subdirectory(benchmark)

Either way, link to the library as follows.

target_link_libraries(MyTarget benchmark::benchmark)