Skip to main content

A library to benchmark code snippets.

Project description

Benchmark

build-and-test bazel test-bindings Coverage Status OpenSSF Scorecard

Discord

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++11. However, it requires C++17 to build, including compiler and standard library support.

See dependencies.md for more details regarding supported compilers and standards.

If you have need for a particular compiler to be supported, patches are very welcome.

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 -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -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.

To enable sanitizer checks (eg., asan and tsan), add:

 -DCMAKE_C_FLAGS="-g -O2 -fno-omit-frame-pointer -fsanitize=address -fsanitize=thread -fno-sanitize-recover=all"
 -DCMAKE_CXX_FLAGS="-g -O2 -fno-omit-frame-pointer -fsanitize=address -fsanitize=thread -fno-sanitize-recover=all "  

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

google_benchmark-1.9.5.tar.gz (15.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

google_benchmark-1.9.5-cp312-abi3-win_amd64.whl (188.8 kB view details)

Uploaded CPython 3.12+Windows x86-64

google_benchmark-1.9.5-cp312-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (210.9 kB view details)

Uploaded CPython 3.12+manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

google_benchmark-1.9.5-cp312-abi3-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl (191.6 kB view details)

Uploaded CPython 3.12+manylinux: glibc 2.26+ ARM64manylinux: glibc 2.28+ ARM64

google_benchmark-1.9.5-cp312-abi3-macosx_11_0_arm64.whl (160.0 kB view details)

Uploaded CPython 3.12+macOS 11.0+ ARM64

google_benchmark-1.9.5-cp312-abi3-macosx_10_14_x86_64.whl (168.9 kB view details)

Uploaded CPython 3.12+macOS 10.14+ x86-64

google_benchmark-1.9.5-cp311-cp311-win_amd64.whl (190.7 kB view details)

Uploaded CPython 3.11Windows x86-64

google_benchmark-1.9.5-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (211.8 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

google_benchmark-1.9.5-cp311-cp311-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl (192.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.26+ ARM64manylinux: glibc 2.28+ ARM64

google_benchmark-1.9.5-cp311-cp311-macosx_11_0_arm64.whl (161.2 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

google_benchmark-1.9.5-cp311-cp311-macosx_10_14_x86_64.whl (169.9 kB view details)

Uploaded CPython 3.11macOS 10.14+ x86-64

google_benchmark-1.9.5-cp310-cp310-win_amd64.whl (190.4 kB view details)

Uploaded CPython 3.10Windows x86-64

google_benchmark-1.9.5-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (211.5 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

google_benchmark-1.9.5-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl (192.3 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.26+ ARM64manylinux: glibc 2.28+ ARM64

google_benchmark-1.9.5-cp310-cp310-macosx_11_0_arm64.whl (160.8 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

google_benchmark-1.9.5-cp310-cp310-macosx_10_14_x86_64.whl (169.5 kB view details)

Uploaded CPython 3.10macOS 10.14+ x86-64

File details

Details for the file google_benchmark-1.9.5.tar.gz.

File metadata

  • Download URL: google_benchmark-1.9.5.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for google_benchmark-1.9.5.tar.gz
Algorithm Hash digest
SHA256 923952ea22e516ca0217311f3c7e5f24ce6916394319e6a595cb813b3aa61d37
MD5 cae502e1132ae9e756f7e12226169698
BLAKE2b-256 5a8c82632a5540fb79c67c8ed144ba9c19639de3e50e4ec19ca635f8e1f7d7ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.5.tar.gz:

Publisher: wheels.yml on google/benchmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file google_benchmark-1.9.5-cp312-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.5-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 daf706babbb8a16e503712b22c8b48acab7ee22da6dde7914cd1153ecadd9d9b
MD5 69ffa8fbafcc7c4ec328814f054b69f3
BLAKE2b-256 7dd0b6a49af3fd9e272cbf16e550ef962100ede41b6ace04ac988565e9262bf9

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.5-cp312-abi3-win_amd64.whl:

Publisher: wheels.yml on google/benchmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file google_benchmark-1.9.5-cp312-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.5-cp312-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0e0efb61240a01da61aaab0c943df96bb769ddbde501c72338d0b5f29751aa89
MD5 aad63d0ff08d038abf613e568e8adb66
BLAKE2b-256 d4642985a833a4679aeef07f0c357b321db838fa5a94abad2b4278a13e1b4000

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.5-cp312-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: wheels.yml on google/benchmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file google_benchmark-1.9.5-cp312-abi3-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.5-cp312-abi3-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 33f7d8bb54ed401938af58150e12b6813d36199e58a2384ab4a17eeac1b57455
MD5 f99fedc7b520f12a58f06b86f57b665a
BLAKE2b-256 6e29373117eb27c60ff3a01770aacb79db8d84c97ab4e1f741cb5841df3b9d14

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.5-cp312-abi3-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl:

Publisher: wheels.yml on google/benchmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file google_benchmark-1.9.5-cp312-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.5-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9d746a55ac17cbed4eaba4febe8e759634bbe13a67e42fa5608d928854590bfd
MD5 6a2bb75b77e5ccd44bf8463ea207a2c7
BLAKE2b-256 4f0e7dc1d350a9b2269af65bdeab1eae23da2b56cbbfb42b1441a620de7abf34

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.5-cp312-abi3-macosx_11_0_arm64.whl:

Publisher: wheels.yml on google/benchmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file google_benchmark-1.9.5-cp312-abi3-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.5-cp312-abi3-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 d28862c2c06e74457ecc407e45f25744de8fd1534504b56a26c1cde77363840b
MD5 ecdc47daee92ed79025cf5b016416388
BLAKE2b-256 4c79f69f30a233b066ee56e13424cdb82271f224cab45c2b966a7aab2afdd27d

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.5-cp312-abi3-macosx_10_14_x86_64.whl:

Publisher: wheels.yml on google/benchmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file google_benchmark-1.9.5-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 65f94de5bf4dfcab85e31cb901227009cbdc5d651d7d66fbd0f636a57f16044b
MD5 d67f46f6ea9dfbc6561cc1d1c835fa56
BLAKE2b-256 dffef105fb10f854b7e88a570c3d7ed2fd08a01586820068be3040defb2ad6f4

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.5-cp311-cp311-win_amd64.whl:

Publisher: wheels.yml on google/benchmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file google_benchmark-1.9.5-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.5-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 822663f8d44c8238aab461218f849c7a24aae3289aadc48cb667992cec106e22
MD5 e3a9c90295de56cd3e6e441f13965574
BLAKE2b-256 bb0840198026a7c5b2721ee0fadc8a9c73c3187057f034b07156f1828739851b

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.5-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: wheels.yml on google/benchmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file google_benchmark-1.9.5-cp311-cp311-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.5-cp311-cp311-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 3df88cc90f0d61e9fbd49ff2ec7d979d1540a75180f8398f793148f78c07ed02
MD5 be14a731610bfa02ed59724372f640e5
BLAKE2b-256 1502e87d6b3a3087597fccd465f615f3256ba1e70a0517797a0e6b2a19645ee0

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.5-cp311-cp311-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl:

Publisher: wheels.yml on google/benchmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file google_benchmark-1.9.5-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.5-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f62465213f4ac9428a19b0233891b6dc599e505fd8713c4e9785a6519e298e2f
MD5 85400bfa0e2966daaacc2d643fca646c
BLAKE2b-256 c7fe3efe420aa9831b312c8a8093ed85eeee38265e527717612e45a89e851ae7

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.5-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: wheels.yml on google/benchmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file google_benchmark-1.9.5-cp311-cp311-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.5-cp311-cp311-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 0fb0caaae5d27bfa980aa470de394faa5cc467553c6125f4394fb4e1ace49526
MD5 f05ebeb58614f1b4dcf11de7913485bd
BLAKE2b-256 a5e13f12868e3327b4b1bb0bae2949c282d12b5d682f05b0299dc431a5d4c71b

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.5-cp311-cp311-macosx_10_14_x86_64.whl:

Publisher: wheels.yml on google/benchmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file google_benchmark-1.9.5-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d8527d7f858f8c94e232ea78a174d83c843a92904b03f524c601a4ca063a80a0
MD5 10acbb1e13703e21625d7bac56ca7038
BLAKE2b-256 ff1e819d990482b28a3520b601a7224e68cdcd46fa1c578eb53143715c31a9b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.5-cp310-cp310-win_amd64.whl:

Publisher: wheels.yml on google/benchmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file google_benchmark-1.9.5-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.5-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 abb5ba063cfdc2fa541e69311240050bff8ecac4ed0793321681a9abb33af3c4
MD5 63ffdbc21a8a13b928bfae17d05004a5
BLAKE2b-256 4a8f167fb34c4b06d24237ff094c1a765489671f3bbf2b3257d051c66eb8e914

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.5-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: wheels.yml on google/benchmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file google_benchmark-1.9.5-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.5-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 5c3d159693b047837926263cc60d455059070155472f6af100394495a01a6f64
MD5 800a9e89b0f509888cf43476f115511d
BLAKE2b-256 2371cdd967b1572839050325770f5f4992a984d2425a920eae5c58531a3bae97

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.5-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl:

Publisher: wheels.yml on google/benchmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file google_benchmark-1.9.5-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.5-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c07c3cb181435507134b36161c45915f25caf92be5240749ca287b2d30de0171
MD5 178e9cea105b3cb47a953877d46549aa
BLAKE2b-256 61c1d08a867fc890a0c80701bd55f5f7753c4d47fed2679e451e6ae9425481c4

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.5-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: wheels.yml on google/benchmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file google_benchmark-1.9.5-cp310-cp310-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.5-cp310-cp310-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 4be16d8f2ddc0b83dbbc7343051f3acbbbcc8803cab9b71853bdba55f5b1e835
MD5 eb35331a8d70fec0d71022a5fbbb3d18
BLAKE2b-256 d12fd481686a52d80c3489285648e65a826d6480be26aeb9348ecf1d7a91ad5e

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.5-cp310-cp310-macosx_10_14_x86_64.whl:

Publisher: wheels.yml on google/benchmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page