Lines Matching refs:benchmark
1 # benchmark chapter
2 …uild Status](https://travis-ci.org/google/benchmark.svg?branch=master)](https://travis-ci.org/goog…
3 …7t1tk7cpxs/branch/master?svg=true)](https://ci.appveyor.com/project/google/benchmark/branch/master)
4 …age Status](https://coveralls.io/repos/google/benchmark/badge.svg)](https://coveralls.io/r/google/…
8 Discussion group: https://groups.google.com/d/forum/benchmark-discuss
22 $ git clone https://github.com/google/benchmark.git
24 $ git clone https://github.com/google/googletest.git benchmark/googletest
26 $ cmake -G <generator> [options] ../benchmark
34 * Checkout the GTest sources into `benchmark/googletest`.
56 git clone https://github.com/google/benchmark.git
57 cd benchmark
70 Now you have google/benchmark installed in your machine
80 [`v2` branch](https://github.com/google/benchmark/tree/v2). Users who wish
91 #include <benchmark/benchmark.h>
93 static void BM_StringCreation(benchmark::State& state) {
97 // Register the function as a benchmark
100 // Define another benchmark
101 static void BM_StringCopy(benchmark::State& state) {
111 Don't forget to inform your linker to add benchmark library e.g. through `-lbenchmark` compilation …
113 The benchmark library will reporting the timing for the code within the `for(...)` loop.
122 static void BM_memcpy(benchmark::State& state) {
138 the specified range and will generate a benchmark for each such argument.
153 You might have a benchmark that depends on two or more inputs. For example, the
158 static void BM_SetInsert(benchmark::State& state) {
181 product of the two specified ranges and will generate a benchmark for each such
190 benchmark. The following example enumerates a dense range on one parameter,
194 static void CustomArguments(benchmark::internal::Benchmark* b) {
208 static void BM_StringCompare(benchmark::State& state) {
212 benchmark::DoNotOptimize(s1.compare(s2));
217 ->RangeMultiplier(2)->Range(1<<10, 1<<18)->Complexity(benchmark::oN);
242 template <class Q> int BM_Sequential(benchmark::State& state) {
258 Three macros are provided for adding benchmark templates.
276 static void BM_Fast(benchmark::State &state) {
294 call benchmark::State::StartKeepRunning()
318 call benchmark::State::StartKeepRunning()
324 the benchmark loop should be preferred.
326 ## Passing arbitrary arguments to a benchmark
327 In C++11 it is possible to define a benchmark that takes an arbitrary number
329 macro creates a benchmark that invokes `func` with the `benchmark::State` as
331 The `test_case_name` is appended to the name of the benchmark and
336 void BM_takes_args(benchmark::State& state, ExtraArgs&&... extra_args) {
339 // Registers a benchmark named "BM_takes_args/int_string_test" that passes
344 avoid modifying global state inside of a benchmark.
351 pointer to a new benchmark with the specified `name` that invokes
352 `func(st, args...)` where `st` is a `benchmark::State` object.
356 benchmark tests to be registered programmatically.
359 as a benchmark. Including capturing lambdas and function objects.
363 auto BM_test = [](benchmark::State& st, auto Inputs) { /* ... */ };
367 benchmark::RegisterBenchmark(test_input.name(), BM_test, test_input);
368 benchmark::Initialize(&argc, argv);
369 benchmark::RunSpecifiedBenchmarks();
374 In a multithreaded test (benchmark invoked by multiple threads simultaneously),
376 the start of the benchmark loop, and all will have finished before any thread
377 exits the benchmark loop. (This behavior is also provided by the `KeepRunning()`
382 static void BM_MultiThreaded(benchmark::State& state) {
413 `SetIterationTime` once per iteration of the benchmark loop to
423 static void BM_ManualTiming(benchmark::State& state) {
447 the `benchmark::DoNotOptimize(...)` and `benchmark::ClobberMemory()`
451 static void BM_test(benchmark::State& state) {
455 benchmark::DoNotOptimize(x += i);
490 static void BM_vector_push_back(benchmark::State& state) {
494 benchmark::DoNotOptimize(v.data()); // Allow v.data() to be clobbered.
496 benchmark::ClobberMemory(); // Force 42 to be written to memory.
504 If a benchmark runs a few milliseconds it may be hard to visually compare the
509 BENCHMARK(BM_test)->Unit(benchmark::kMillisecond);
513 In all cases, the number of iterations for which the benchmark is run is
514 governed by the amount of time the benchmark takes. Concretely, the number of
517 set as a flag `--benchmark_min_time` or per benchmark by calling `MinTime` on
518 the registered benchmark object.
521 By default each benchmark is run once and that single result is reported.
524 benchmark.
526 The number of runs of each benchmark is specified globally by the
527 `--benchmark_repetitions` flag or on a per benchmark basis by calling
528 `Repetitions` on the registered benchmark object. When a benchmark is run more
535 Calling `ReportAggregatesOnly(bool)` on a registered benchmark object overrides
536 the value of the flag for that benchmark.
546 void BM_spin_empty(benchmark::State& state) {
549 benchmark::DoNotOptimize(x);
563 first defining a type that derives from `::benchmark::Fixture` and then
573 class MyFixture : public benchmark::Fixture {};
575 BENCHMARK_F(MyFixture, FooTest)(benchmark::State& st) {
581 BENCHMARK_DEFINE_F(MyFixture, BarTest)(benchmark::State& st) {
600 class MyFixture : public benchmark::Fixture {};
602 BENCHMARK_TEMPLATE_F(MyFixture, IntTest, int)(benchmark::State& st) {
608 BENCHMARK_TEMPLATE_DEFINE_F(MyFixture, DoubleTest, double)(benchmark::State& st) {
623 static void UserCountersExample1(benchmark::State& state) {
640 When the benchmark finishes, the counters from each thread will be summed;
641 the resulting sum is the value which will be shown for the benchmark.
652 // by the duration of the benchmark.
653 state.counters["FooRate"] = Counter(numFoos, benchmark::Counter::kIsRate);
657 state.counters["FooAvg"] = Counter(numFoos, benchmark::Counter::kAvgThreads);
660 state.counters["FooAvgRate"] = Counter(numFoos,benchmark::Counter::kAvgThreadsRate);
680 or where there are only a couple of lines per benchmark. Here's an example of
704 passing the flag `--benchmark_counters_tabular=true` to the benchmark
706 a lot of lines per individual benchmark. Note that this will trigger a
738 Note above the additional header printed when the benchmark changes from
745 communication, occur within a benchmark the
747 of benchmark and report the error. Note that only future iterations of the
748 `KeepRunning()` are skipped. For the ranged-for version of the benchmark loop
750 Users may explicitly return to exit the benchmark immediately.
752 The `SkipWithError(...)` function may be used at any point within the benchmark,
753 including before and after the benchmark loop.
758 static void BM_test(benchmark::State& state) {
774 static void BM_test_ranged_fo(benchmark::State & state) {
820 The `benchmarks` attribute contains a list of ever benchmark run. Example json
870 The library supports writing the output of the benchmark to a file specified
876 By default, benchmark builds as a debug library. You will see a warning in the output when this is …
894 See [issue #67](https://github.com/google/benchmark/issues/67) for more details.
916 ***WARNING*** CPU scaling is enabled, the benchmark real time measurements may be noisy and will in…
918 you might want to disable the CPU frequency scaling while running the benchmark: