Skip to main content

A library to benchmark code snippets.

Project description

Benchmark

build-and-test bazel pylint test-bindings Coverage Status

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.4.tar.gz (23.0 kB view details)

Uploaded Source

Built Distributions

google_benchmark-1.9.4-cp312-abi3-win_amd64.whl (193.4 kB view details)

Uploaded CPython 3.12+Windows x86-64

google_benchmark-1.9.4-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (220.1 kB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ x86-64

google_benchmark-1.9.4-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (209.6 kB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ ARM64

google_benchmark-1.9.4-cp312-abi3-macosx_11_0_arm64.whl (167.0 kB view details)

Uploaded CPython 3.12+macOS 11.0+ ARM64

google_benchmark-1.9.4-cp312-abi3-macosx_10_14_x86_64.whl (176.9 kB view details)

Uploaded CPython 3.12+macOS 10.14+ x86-64

google_benchmark-1.9.4-cp311-cp311-win_amd64.whl (195.0 kB view details)

Uploaded CPython 3.11Windows x86-64

google_benchmark-1.9.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (221.0 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

google_benchmark-1.9.4-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (210.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

google_benchmark-1.9.4-cp311-cp311-macosx_11_0_arm64.whl (167.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

google_benchmark-1.9.4-cp311-cp311-macosx_10_14_x86_64.whl (177.8 kB view details)

Uploaded CPython 3.11macOS 10.14+ x86-64

google_benchmark-1.9.4-cp310-cp310-win_amd64.whl (194.8 kB view details)

Uploaded CPython 3.10Windows x86-64

google_benchmark-1.9.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (220.5 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

google_benchmark-1.9.4-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (210.3 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ ARM64

google_benchmark-1.9.4-cp310-cp310-macosx_11_0_arm64.whl (167.4 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

google_benchmark-1.9.4-cp310-cp310-macosx_10_14_x86_64.whl (177.5 kB view details)

Uploaded CPython 3.10macOS 10.14+ x86-64

File details

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

File metadata

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

File hashes

Hashes for google_benchmark-1.9.4.tar.gz
Algorithm Hash digest
SHA256 7e835235179eb0dd6efbd2f37d640ff662a4e6573ae0b1e1aad20ed071723683
MD5 1a23ffce2b4a5cef2643dc54243ad308
BLAKE2b-256 19b86c8503eec09c486c15dfcc0031aea9f99a3366292cd034b5e15a7b6e19c2

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.4.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.4-cp312-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.4-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 9479274b49589a77f83ad052b25bf93b3050f00c71ab8bc4a3a57a7dbd978b57
MD5 6cd74481f3c5485f48473709c471c5d8
BLAKE2b-256 428807e4a8be8ac54aea1cb7140dcba5f0d83788fc0d040ef60c11bac0653def

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.4-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.4-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.4-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 de2c94b90cbbe221b0cb0029872788cfe55eacc8165f9ef5f9bb523110813e3a
MD5 82a8d0880bfb976a77f41890a7b378b4
BLAKE2b-256 f9d149cee9e95a0fed58547b7b6056b50eded014b196f0931b64154fed513057

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.4-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_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.4-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.4-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 806c58a69b977822990fb0501c1fc1c03342bb636e70fc3baa074aba28df9d88
MD5 05bf560c6e84b2dc6c2f5acd38686e85
BLAKE2b-256 c2442cb77d92c409e569cc75ba28837f24abdbc8301c04a2977ae3f5a91769b9

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.4-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_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.4-cp312-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.4-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2f1bc5d29d3c9dfc616ecc95f5e4f0aab00383c9756008820ba36b9be2e62744
MD5 4cbc391574d005dbbcfd405ae203312d
BLAKE2b-256 f67c04b41b179f046204dad09aad1ba5fb0716f8f6343318909e264d36b03c93

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.4-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.4-cp312-abi3-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.4-cp312-abi3-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 d560b4c4242a2958aa386a897afb4b873ea441d615ffc33bea939694a0eaa577
MD5 63dcb75a978e1b8b5a87227d97823fab
BLAKE2b-256 92a3cb6641e12fa7abae04b0ccc59bbac8df0f837ba7183ae7ae0904d20ec78f

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.4-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.4-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 b56c055fa93e928c485c3bb56b41a44e31bfe2001ef6a9555c913d2f858f95fa
MD5 6abbf32b39658ee9b2e40acf05131c5e
BLAKE2b-256 7625725cb0c76be788d2aa1e7c66cc0179a041ef32e8b485f934c3572fddc24c

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.4-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.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 044a5f228b3d3f4c977dccd0277fe8c7799a22d9da4c2d5e26e7999bcc2d3b13
MD5 45478c9712b6fc891025969eda73c41e
BLAKE2b-256 9b9135651bbf53df155d082b95ebc6e5b3059e6616eb6e052611355c512bec73

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_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.4-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.4-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 522fbd4bc700194d254ea919876e3de3c5cd29884e17bd65d31f215ff815ecd8
MD5 9baedf53251433dd216b1d1909975d35
BLAKE2b-256 0d4e6f434546a39161b3db33efb9dce8c47fa724332f5fad8ed921b2f1c64d86

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.4-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_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.4-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1e8066d47927270f27fb131f92f0ef11aff75d6f8e287a8e61923993133de232
MD5 515f88d79b3c99e95acda85eb1767afe
BLAKE2b-256 ff9176318b08b324b8e1ca231f9edc0a6e39ca0df8a67ba30804d93519568d2f

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.4-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.4-cp311-cp311-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.4-cp311-cp311-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 a50927c5875a2965a3a4ece16b2ad899d2a1a11c67da0afd73690c2e19bd8b86
MD5 a09f8070579155f627d1ed9c8c9b5cb9
BLAKE2b-256 b9b9a7caa910c4ccc3b9da82f7c3cb246440bcd5e715c01eeed7899ca3645295

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.4-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.4-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 158a7be7cc887fa3d9d22ed73d7d73c467581ae441af876e2cf8a62b85c6dc75
MD5 34705ac9d7d21cfa78e23312425c9361
BLAKE2b-256 67932f618f53ea0165886b242a7645fcccaaab329c3abdb3958265d41c18f5b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.4-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.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b0e0669e59cd856659241417a36f797998615c79a8dc155c8813243583bd9742
MD5 2cd0f3fa55f1ee6bf5e3af495b4dcde2
BLAKE2b-256 aec1709a743371c14d5ddb85fd3438794f511e13502ac3e31c27b2d9d6439f4e

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_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.4-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.4-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 43927e1b0b211580bb3ddb31fb78aa583a7ca7263dae4cc4f0fbddbbcbea0a32
MD5 17c268d185f8d00c0d8e5fbf5fe23f3b
BLAKE2b-256 9cde1650a27a8368923c1ba38f0ac02644913592796080bd618b5e2c894b58fa

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.4-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_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.4-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 31fb115d019640e476b25f5e7a0eb09a171b1b656925e5fe9abdba9ffeaca52f
MD5 af0b2f006f55c4090f2c2cb1fcf30731
BLAKE2b-256 bd3a325e102f2bff6b21e88286f7a36d64fe40bcc5806ee8bf0514d9e2ab9111

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.4-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.4-cp310-cp310-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.9.4-cp310-cp310-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 55ef51f5da165256376596ab36a33c83beedc73d123b81f628d53aae127ef875
MD5 2de06d35b53d8678fd00d2d530673287
BLAKE2b-256 f3b4d4e5bd44fdbddf6fe40b8721823a28475108fe1843f4d4ba891277bccd8f

See more details on using hashes here.

Provenance

The following attestation bundles were made for google_benchmark-1.9.4-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 Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page