Package: google-benchmark
BenchmarkA library to benchmark code snippets, similar to unit tests. Example:#include<benchmark/benchmark.h>staticvoidBM_SomeFunction(benchmark::State&state){// Perform setup herefor(auto_:state){// This code gets timedSomeFunction();}}// Register the function as a benchmarkBENCHMARK(BM_SomeFunction);// Run the benchmarkBENCHMARK_MAIN();Getting StartedTo get started, seeRequirementsandInstallation. SeeUsagefor a full example and theUser Guidefor a more comprehensive feature overview.It may also help to read theGoogle Test documentationas some of the structural aspects of the APIs are similar.ResourcesDiscussion groupIRC channels:libera#benchmarkAdditional Tooling DocumentationAssembly Testing DocumentationBuilding and installing Python bindingsRequirementsThe 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.8Clang 3.4Visual Studio 14 2015Intel 2015 Update 1SeePlatform-Specific Build Instructions.InstallationThis describes the installation process using cmake. As pre-requisites, you'll
need git and cmake installed.Seedependencies.mdfor more details regarding supported
versions of build tools.# Check out the library.$gitclonehttps://github.com/google/benchmark.git# Go to the library root directory$cdbenchmark# Make a build directory to place the build output.$cmake-Emake_directory"build"# Generate build system files with cmake, and download any dependencies.$cmake-Echdir"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"--configReleaseThis builds thebenchmarkandbenchmark_mainlibraries 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-Echdir"build"ctest--build-configReleaseIf you want to install the library globally, also run:sudo cmake --build "build" --config Release --target installNote that Google Benchmark requires Google Test to build and run the tests. This
dependency can be provided two ways:Checkout the Google Test sources intobenchmark/googletest.Otherwise, if-DBENCHMARK_DOWNLOAD_DEPENDENCIES=ONis 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=OFFtoCMAKE_ARGS.Debug vs ReleaseBy 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=Releasewhen generating the build system files, as shown
above. The use of--config Releasein 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=truewhen
generating the build system files.If you are using gcc, you might need to setGCC_ARandGCC_RANLIBcmake
cache variables, if autodetection fails.If you are using clang, you may need to setLLVMAR_EXECUTABLE,LLVMNM_EXECUTABLEandLLVMRANLIB_EXECUTABLEcmake cache variables.To enable sanitizer checks (eg.,asanandtsan), 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 VersionsThe 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 thev2branch. 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.UsageBasic usageDefine a function that executes the code to measure, register it as a benchmark
function using theBENCHMARKmacro, and ensure an appropriatemainfunction
is available:#include<benchmark/benchmark.h>staticvoidBM_StringCreation(benchmark::State&state){for(auto_:state)std::stringempty_string;}// Register the function as a benchmarkBENCHMARK(BM_StringCreation);// Define another benchmarkstaticvoidBM_StringCopy(benchmark::State&state){std::stringx="hello";for(auto_:state)std::stringcopy(x);}BENCHMARK(BM_StringCopy);BENCHMARK_MAIN();To run the benchmark, compile and link against thebenchmarklibrary
(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-isystembenchmark/include\-Lbenchmark/build/src-lbenchmark-lpthread-omybenchmarkAlternatively, link against thebenchmark_mainlibrary and removeBENCHMARK_MAIN();above to get the same behavior.The compiled executable will run all benchmarks by default. Pass the--helpflag for option information or see theUser Guide.Usage with CMakeIf using CMake, it is recommended to link against the project-providedbenchmark::benchmarkandbenchmark::benchmark_maintargets usingtarget_link_libraries.
It is possible to usefind_packageto import an installed version of the
library.find_package(benchmarkREQUIRED)Alternatively,add_subdirectorywill incorporate the library directly in
to one's CMake project.add_subdirectory(benchmark)Either way, link to the library as follows.target_link_libraries(MyTargetbenchmark::benchmark)