Lines Matching full:benchmark
0 # benchmark
2 [](https://travis-ci.org/google/benchmark)
3 [](https://ci.appveyor.com/project/google/benchmark/branch/master)
4 [](https://coveralls.io/r/google/benchmark)
9 Discussion group: https://groups.google.com/d/forum/benchmark-discuss
23 $ git clone https://github.com/google/benchmark.git
24 # Benchmark requires GTest as a dependency. Add the source tree as a subdirectory.
25 $ git clone https://github.com/google/googletest.git benchmark/googletest
27 $ cmake -G <generator> [options] ../benchmark
32 Note that Google Benchmark requires GTest to build and run the tests. This
35 * Checkout the GTest sources into `benchmark/googletest`.
60 git clone https://github.com/google/benchmark.git
61 cd benchmark
74 Now you have google/benchmark installed in your machine
84 [`v2` branch](https://github.com/google/benchmark/tree/v2). Users who wish
95 #include <benchmark/benchmark.h>
97 static void BM_StringCreation(benchmark::State& state) {
101 // Register the function as a benchmark
102 BENCHMARK(BM_StringCreation);
104 // Define another benchmark
105 static void BM_StringCopy(benchmark::State& state) {
110 BENCHMARK(BM_StringCopy);
115 Don't forget to inform your linker to add benchmark library e.g. through `-lbenchmark` compilation flag.
117 The benchmark library will reporting the timing for the code within the `for(...)` loop.
126 static void BM_memcpy(benchmark::State& state) {
137 BENCHMARK(BM_memcpy)->Arg(8)->Arg(64)->Arg(512)->Arg(1<<10)->Arg(8<<10);
142 the specified range and will generate a benchmark for each such argument.
145 BENCHMARK(BM_memcpy)->Range(8, 8<<10);
153 BENCHMARK(BM_memcpy)->RangeMultiplier(2)->Range(8, 8<<10);
157 You might have a benchmark that depends on two or more inputs. For example, the
162 static void BM_SetInsert(benchmark::State& state) {
172 BENCHMARK(BM_SetInsert)
185 product of the two specified ranges and will generate a benchmark for each such
189 BENCHMARK(BM_SetInsert)->Ranges({{1<<10, 8<<10}, {128, 512}});
194 benchmark. The following example enumerates a dense range on one parameter,
198 static void CustomArguments(benchmark::internal::Benchmark* b) {
203 BENCHMARK(BM_SetInsert)->Apply(CustomArguments);
212 static void BM_StringCompare(benchmark::State& state) {
216 benchmark::DoNotOptimize(s1.compare(s2));
220 BENCHMARK(BM_StringCompare)
221 ->RangeMultiplier(2)->Range(1<<10, 1<<18)->Complexity(benchmark::oN);
228 BENCHMARK(BM_StringCompare)
236 BENCHMARK(BM_StringCompare)->RangeMultiplier(2)
246 template <class Q> int BM_Sequential(benchmark::State& state) {
262 Three macros are provided for adding benchmark templates.
280 static void BM_Fast(benchmark::State &state) {
285 BENCHMARK(BM_Fast);
298 call benchmark::State::StartKeepRunning()
322 call benchmark::State::StartKeepRunning()
328 the benchmark loop should be preferred.
330 ## Passing arbitrary arguments to a benchmark
331 In C++11 it is possible to define a benchmark that takes an arbitrary number
333 macro creates a benchmark that invokes `func` with the `benchmark::State` as
335 The `test_case_name` is appended to the name of the benchmark and
340 void BM_takes_args(benchmark::State& state, ExtraArgs&&... extra_args) {
343 // Registers a benchmark named "BM_takes_args/int_string_test" that passes
348 avoid modifying global state inside of a benchmark.
355 pointer to a new benchmark with the specified `name` that invokes
356 `func(st, args...)` where `st` is a `benchmark::State` object.
358 Unlike the `BENCHMARK` registration macros, which can only be used at the global
360 benchmark tests to be registered programmatically.
363 as a benchmark. Including capturing lambdas and function objects.
367 auto BM_test = [](benchmark::State& st, auto Inputs) { /* ... */ };
371 benchmark::RegisterBenchmark(test_input.name(), BM_test, test_input);
372 benchmark::Initialize(&argc, argv);
373 benchmark::RunSpecifiedBenchmarks();
378 In a multithreaded test (benchmark invoked by multiple threads simultaneously),
380 the start of the benchmark loop, and all will have finished before any thread
381 exits the benchmark loop. (This behavior is also provided by the `KeepRunning()`
386 static void BM_MultiThreaded(benchmark::State& state) {
397 BENCHMARK(BM_MultiThreaded)->Threads(2);
405 BENCHMARK(BM_test)->Range(8, 8<<10)->UseRealTime();
417 `SetIterationTime` once per iteration of the benchmark loop to
427 static void BM_ManualTiming(benchmark::State& state) {
446 BENCHMARK(BM_ManualTiming)->Range(1, 1<<17)->UseManualTime();
451 the `benchmark::DoNotOptimize(...)` and `benchmark::ClobberMemory()`
455 static void BM_test(benchmark::State& state) {
459 benchmark::DoNotOptimize(x += i);
494 static void BM_vector_push_back(benchmark::State& state) {
498 benchmark::DoNotOptimize(v.data()); // Allow v.data() to be clobbered.
500 benchmark
508 If a benchmark runs a few milliseconds it may be hard to visually compare the
513 BENCHMARK(BM_test)->Unit(benchmark::kMillisecond);
517 In all cases, the number of iterations for which the benchmark is run is
518 governed by the amount of time the benchmark takes. Concretely, the number of
521 set as a flag `--benchmark_min_time` or per benchmark by calling `MinTime` on
522 the registered benchmark object.
525 By default each benchmark is run once and that single result is reported.
528 benchmark.
530 The number of runs of each benchmark is specified globally by the
531 `--benchmark_repetitions` flag or on a per benchmark basis by calling
532 `Repetitions` on the registered benchmark object. When a benchmark is run more
539 Calling `ReportAggregatesOnly(bool)` on a registered benchmark object overrides
540 the value of the flag for that benchmark.
550 void BM_spin_empty(benchmark::State& state) {
553 benchmark::DoNotOptimize(x);
558 BENCHMARK(BM_spin_empty)
567 first defining a type that derives from `::benchmark::Fixture` and then
577 class MyFixture : public benchmark::Fixture {};
579 BENCHMARK_F(MyFixture, FooTest)(benchmark::State& st) {
585 BENCHMARK_DEFINE_F(MyFixture, BarTest)(benchmark::State& st) {
604 class MyFixture : public benchmark::Fixture {};
606 BENCHMARK_TEMPLATE_F(MyFixture, IntTest, int)(benchmark::State& st) {
612 BENCHMARK_TEMPLATE_DEFINE_F(MyFixture, DoubleTest, double)(benchmark::State& st) {
627 static void UserCountersExample1(benchmark::State& state) {
644 When the benchmark finishes, the counters from each thread will be summed;
645 the resulting sum is the value which will be shown for the benchmark.
656 // by the duration of the benchmark.
657 state.counters["FooRate"] = Counter(numFoos, benchmark::Counter::kIsRate);
661 state.counters["FooAvg"] = Counter(numFoos, benchmark::Counter::kAvgThreads);
664 state.counters["FooAvgRate"] = Counter(numFoos,benchmark::Counter::kAvgThreadsRate);
684 or where there are only a couple of lines per benchmark. Here's an example of
689 Benchmark Time CPU Iterations UserCounters...
708 passing the flag `--benchmark_counters_tabular=true` to the benchmark
710 a lot of lines per individual benchmark. Note that this will trigger a
717 Benchmark Time CPU Iterations Bar Bat Baz Foo
728 Benchmark Time CPU Iterations
742 Note above the additional header printed when the benchmark changes from
749 communication, occur within a benchmark the
751 of benchmark and report the error. Note that only future iterations of the
752 `KeepRunning()` are skipped. For the ranged-for version of the benchmark loop
754 Users may explicitly return to exit the benchmark immediately.
756 The `SkipWithError(...)` function may be used at any point within the benchmark,
757 including before and after the benchmark loop.
762 static void BM_test(benchmark::State& state) {
778 static void BM_test_ranged_fo(benchmark::State & state) {
796 Benchmark Time CPU Iterations
814 Benchmark Time(ns) CPU(ns) Iterations
824 The `benchmarks` attribute contains a list of ever benchmark run. Example json
874 The library supports writing the output of the benchmark to a file specified
880 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, use:
898 See [issue #67](https://github.com/google/benchmark/issues/67) for more details.
902 Google Benchmark uses C++11 when building the library. As such we require
920 ***WARNING*** CPU scaling is enabled, the benchmark real time measurements may be noisy and will incur extra overhead.
922 benchmark: