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++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.

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

Uploaded Source

Built Distributions

google_benchmark-1.8.3-cp311-cp311-win_amd64.whl (183.8 kB view details)

Uploaded CPython 3.11 Windows x86-64

google_benchmark-1.8.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (253.4 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

google_benchmark-1.8.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (245.0 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

google_benchmark-1.8.3-cp311-cp311-macosx_11_0_arm64.whl (188.6 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

google_benchmark-1.8.3-cp311-cp311-macosx_10_14_x86_64.whl (194.1 kB view details)

Uploaded CPython 3.11 macOS 10.14+ x86-64

google_benchmark-1.8.3-cp310-cp310-win_amd64.whl (184.0 kB view details)

Uploaded CPython 3.10 Windows x86-64

google_benchmark-1.8.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (253.7 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

google_benchmark-1.8.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (245.2 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

google_benchmark-1.8.3-cp310-cp310-macosx_11_0_arm64.whl (188.9 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

google_benchmark-1.8.3-cp310-cp310-macosx_10_14_x86_64.whl (194.5 kB view details)

Uploaded CPython 3.10 macOS 10.14+ x86-64

google_benchmark-1.8.3-cp39-cp39-win_amd64.whl (184.2 kB view details)

Uploaded CPython 3.9 Windows x86-64

google_benchmark-1.8.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (253.6 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

google_benchmark-1.8.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (245.2 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

google_benchmark-1.8.3-cp39-cp39-macosx_11_0_arm64.whl (188.9 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

google_benchmark-1.8.3-cp39-cp39-macosx_10_14_x86_64.whl (194.4 kB view details)

Uploaded CPython 3.9 macOS 10.14+ x86-64

google_benchmark-1.8.3-cp38-cp38-win_amd64.whl (184.3 kB view details)

Uploaded CPython 3.8 Windows x86-64

google_benchmark-1.8.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (253.2 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

google_benchmark-1.8.3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (244.9 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

google_benchmark-1.8.3-cp38-cp38-macosx_11_0_arm64.whl (188.4 kB view details)

Uploaded CPython 3.8 macOS 11.0+ ARM64

google_benchmark-1.8.3-cp38-cp38-macosx_10_14_x86_64.whl (193.6 kB view details)

Uploaded CPython 3.8 macOS 10.14+ x86-64

File details

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

File metadata

  • Download URL: google_benchmark-1.8.3.tar.gz
  • Upload date:
  • Size: 21.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.5

File hashes

Hashes for google_benchmark-1.8.3.tar.gz
Algorithm Hash digest
SHA256 71152a826b162146473a06015eefa9f066e19b316a06826fbf25386615653a64
MD5 db111ec90587b837c5a8a38544e610cd
BLAKE2b-256 695bb0db401d0c2fd946aed59dc1c5ddb0fabc699ba8ce6cafa4b866ea5c5e5a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 9f3432a57177f7a46608a07551d50edfe608da344aca07d476a888fb36438650
MD5 e6b3c339a54cbae57218a6db5499c087
BLAKE2b-256 a00b2cc7ae479082c5f8a3eef52223d9afeb8584728ab37c75b1375a8b5e57ce

See more details on using hashes here.

File details

Details for the file google_benchmark-1.8.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ba0547b1075a290e3432025bb544b02f7c717c30e31f696f82907571cb5e2be9
MD5 1275c59b705179c06357884128b297e5
BLAKE2b-256 90cc71e3e0c4f61ed86c47d5116c3e28156a6aab984f0fbbca6eb16511ac11fd

See more details on using hashes here.

File details

Details for the file google_benchmark-1.8.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d2ea4544d3e17a6f87432bc97e79fea23490d9c7c4d10ebd213acf6a40bd1b61
MD5 5a5b0474549dccbecba08175e92b7a30
BLAKE2b-256 1c49b296c3a06d56c5dd37f717cf25366442a548deed3efca9716af12c229f56

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 063f6df1ed384e4dc881ac96644153c18ed755f1a2ed32272534a110bdf14871
MD5 5f79725da8a0aaf19a54b85fa3790b31
BLAKE2b-256 727373b462856e74b7b8a3362780f4dbe976c0dc656dfab62539dcac46087f0d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp311-cp311-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 93e9ef9abf9f9e845a2141935bbcee5e42a7bedc3efb14072adc0310a8b49072
MD5 8d7a79667634355707816d45db008622
BLAKE2b-256 1c97c12d816f10c7a1b5391e76f1b7f355792720fd7e2599f63d41a96ea8fb3a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d5d31bbbec9ebe9a1bab34a631a35988c424ef55ea14055238bc77f7d8f19836
MD5 12458d903b1a7f3c49730b99170b93cf
BLAKE2b-256 7d1ce96ac0dabb1bdd8e95ae0415f78a123af43bfbb4ae626f2f1ce07940c7bb

See more details on using hashes here.

File details

Details for the file google_benchmark-1.8.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 731f1881b757df18add80566ae796b6da101935ea1f45932d1ee094d5fb85b46
MD5 acffdf49a577cc6c8c25155b98b624e0
BLAKE2b-256 87af6eec657891d214d9c21d49bc7f4464f634c64d7873bb82d3d6c50342763d

See more details on using hashes here.

File details

Details for the file google_benchmark-1.8.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c476005b9e7f32c45000719b7c8c2fa95ddcfc058af8d08052eb73692d143619
MD5 60716cbca4757ea7bb0efef797499db0
BLAKE2b-256 d37cf8f04f2669329b4528720fcc976cd90bdd0749603403d93bcacf0f97c4ba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e69bd848173557ed3762830725bff00c2a92de974189a54bd77485bb8bcb18f4
MD5 4d0982f396a0c72ed56710d949b5a3cf
BLAKE2b-256 bf61004bd726aed14468af9e96c22f925e752cc97c6c6cff1be126e6cd71995a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp310-cp310-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 aa3354bb71dc3a32672d1c7fd0621f4967c519213c018dd8e20a9d9e6fb2ae7b
MD5 4432fe4f07c49026bcad1675a6003aaa
BLAKE2b-256 1061a62ba27fc9c07b974e6098e60fcd3469a634dbcb505f19dd9eae984a2a70

See more details on using hashes here.

File details

Details for the file google_benchmark-1.8.3-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 ab8212aaadc39b5aaa0afc063b64959ca93271cf6a72852f0d0aad26f9ae9f24
MD5 76c7952cfa45d4c7befa72b6fbfb6ea7
BLAKE2b-256 ef87c9db02488b01e9532dbdbdfafe621cc858298c28b1805d46f1bbf20d2b55

See more details on using hashes here.

File details

Details for the file google_benchmark-1.8.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5c4786323817112303edf7fd70dc60d1aa15c175d1c9e2c63d71292bb3e51828
MD5 c621e94f3849b472a824de1211989b10
BLAKE2b-256 c59440bb6a76947c73e2fb546421d69ef9211c335c9eddee15037722e5af5ec4

See more details on using hashes here.

File details

Details for the file google_benchmark-1.8.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 902d6e6da560a716ba709c6b55f8585f1aa64a76711b9a1f068e064567f58a4a
MD5 4596445584dc1f9d6419ffd3053e6c1a
BLAKE2b-256 dd8163227d54b24fac82dab20b7f21a94a6dbce2c204c4f2ba3073b14a155d6d

See more details on using hashes here.

File details

Details for the file google_benchmark-1.8.3-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 066b69f809fd0ebc697c90075d1194e4c4ada117811731431523f821b421b28f
MD5 8ea27eab9764dae63b0c107bf459f21a
BLAKE2b-256 af11194e456994cca3d657557c5172134a43aac4aafde5906548194fd97b717a

See more details on using hashes here.

File details

Details for the file google_benchmark-1.8.3-cp39-cp39-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 fc4faa364f22ef81b7d3e9f4ecc6ad62f28d68c47008002aa64474b941b1c76c
MD5 8d125ecd40ef84c1b71ac41b49a1b365
BLAKE2b-256 fb64ab4e1108943ca475e196ab905537ec98009d36c89f50dd7015fe41dbfdbf

See more details on using hashes here.

File details

Details for the file google_benchmark-1.8.3-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 f23a591951c59100e30d97b7ba222072f544d318f470420e21872dee40a4aff0
MD5 6fee6a68f9d90e28405f5c659ff5de7b
BLAKE2b-256 2586aa5388df46c8c590e81f8547de98cbd3d191391bc20e24bfd2f536cb1862

See more details on using hashes here.

File details

Details for the file google_benchmark-1.8.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2b3bb7905233dec505de5cff35e0725b190f411d16ae97e9050073bf9c79cf2a
MD5 bf18b1b1ce0792ea6ee34444605debce
BLAKE2b-256 26602f9028be38c67e712c6063690d160164a31d0086088c808b74cbe1976cb2

See more details on using hashes here.

File details

Details for the file google_benchmark-1.8.3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 9e1d39431e2a5d0960676c3f62180f48c0cb2802c42895eaf5541b7029c20301
MD5 3fcad2972778e20d151be29fa0f73b13
BLAKE2b-256 fcb0d4aa86cbfe782543a08d3b628aa4be76e8f93966c8d6f85a5c22afd2149f

See more details on using hashes here.

File details

Details for the file google_benchmark-1.8.3-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d1504fd53e936d70f438e474c2e87fd94f81bd74a5ae855b1e40d1f9994cdbeb
MD5 10fcda779c038c1cf5c05f638ef6d2f2
BLAKE2b-256 cc5b5ed22453d4df215070dab941f727878af83191d21b13477be327efc96e66

See more details on using hashes here.

File details

Details for the file google_benchmark-1.8.3-cp38-cp38-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for google_benchmark-1.8.3-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 fb014cb611e929d2c2696b009f51ac657c24f706881f3123f10c810b11ba378b
MD5 2aabb2ebeb0c014dd398ab3f466dc1a4
BLAKE2b-256 702ae37a2cd12daaeac8fe779f7afdd4e45e925784049aa21a1419289bc3cca0

See more details on using hashes here.

Supported by

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