在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
BenchmarkA 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 benchmarkBENCHMARK(BM_SomeFunction);// Run the benchmarkBENCHMARK_MAIN(); Getting StartedTo get started, see Requirements andInstallation. See Usage for a full example and theUser Guide for a more comprehensive feature overview. It may also help to read the Google Test documentationas some of the structural aspects of the APIs are similar. ResourcesIRC channels:
Additional Tooling Documentation Assembly Testing Documentation RequirementsThe 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:
See Platform-Specific Build Instructions. InstallationThis describes the installation process using cmake. As pre-requisites, you'llneed git and cmake installed. See dependencies.md for more details regarding supportedversions 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 /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. Thisdependency can be provided two ways:
If you do not wish to build and run the tests, add Debug vs ReleaseBy default, benchmark builds as a debug library. You will see a warning in theoutput when this is the case. To build it as a release library instead, add To enable link-time optimisation, also add If you are using gcc, you might need to set If you are using clang, you may need to set 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 changesbeing made only upon the release of a new major version. Newer, experimental, features are implemented and tested on the UsageBasic usageDefine a function that executes the code to measure, register it as a benchmarkfunction using the #include <benchmark/benchmark.h>static void BM_StringCreation(benchmark::State& state) { for (auto _ : state) std::string empty_string;}// Register the function as a benchmarkBENCHMARK(BM_StringCreation);// Define another benchmarkstatic 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 # 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 The compiled executable will run all benchmarks by default. Pass the Usage with CMakeIf using CMake, it is recommended to link against the project-provided find_package(benchmark REQUIRED) Alternatively, add_subdirectory(benchmark) Either way, link to the library as follows. target_link_libraries(MyTarget benchmark::benchmark) |
请发表评论