• Home
Name Date Size #Lines LOC

..--

cmake/03-May-2024-218192

docs/03-May-2024-6050

include/benchmark/03-May-2024-1,252573

src/03-May-2024-4,8153,368

test/03-May-2024-2,8862,217

tools/03-May-2024-504450

.clang-formatD03-May-202452 64

.gitignoreD03-May-2024469 4738

.travis-libcxx-setup.shD03-May-20241.1 KiB2919

.travis.ymlD03-May-20244.6 KiB158144

.ycm_extra_conf.pyD03-May-20243.6 KiB11676

AUTHORSD03-May-20241.4 KiB4139

Android.bpD03-May-20241.3 KiB5046

CMakeLists.txtD03-May-20247.8 KiB203179

CONTRIBUTING.mdD03-May-20242.4 KiB5941

CONTRIBUTORSD03-May-20242.2 KiB5957

LICENSED03-May-202411.1 KiB203169

MODULE_LICENSE_APACHE2D03-May-20240

NOTICED03-May-202411.1 KiB203169

OWNERSD03-May-202419 21

README.mdD03-May-202426.2 KiB727606

README.versionD03-May-2024140 54

appveyor.ymlD03-May-20241.3 KiB5741

mingw.pyD03-May-202410.1 KiB321281

README.md

1# benchmark
2[![Build Status](https://travis-ci.org/google/benchmark.svg?branch=master)](https://travis-ci.org/google/benchmark)
3[![Build status](https://ci.appveyor.com/api/projects/status/u0qsyp7t1tk7cpxs/branch/master?svg=true)](https://ci.appveyor.com/project/google/benchmark/branch/master)
4[![Coverage Status](https://coveralls.io/repos/google/benchmark/badge.svg)](https://coveralls.io/r/google/benchmark)
5
6A library to support the benchmarking of functions, similar to unit-tests.
7
8Discussion group: https://groups.google.com/d/forum/benchmark-discuss
9
10IRC channel: https://freenode.net #googlebenchmark
11
12[Known issues and common problems](#known-issues)
13
14[Additional Tooling Documentation](docs/tools.md)
15
16## Example usage
17### Basic usage
18Define a function that executes the code to be measured.
19
20```c++
21static void BM_StringCreation(benchmark::State& state) {
22  while (state.KeepRunning())
23    std::string empty_string;
24}
25// Register the function as a benchmark
26BENCHMARK(BM_StringCreation);
27
28// Define another benchmark
29static void BM_StringCopy(benchmark::State& state) {
30  std::string x = "hello";
31  while (state.KeepRunning())
32    std::string copy(x);
33}
34BENCHMARK(BM_StringCopy);
35
36BENCHMARK_MAIN();
37```
38
39### Passing arguments
40Sometimes a family of benchmarks can be implemented with just one routine that
41takes an extra argument to specify which one of the family of benchmarks to
42run. For example, the following code defines a family of benchmarks for
43measuring the speed of `memcpy()` calls of different lengths:
44
45```c++
46static void BM_memcpy(benchmark::State& state) {
47  char* src = new char[state.range(0)];
48  char* dst = new char[state.range(0)];
49  memset(src, 'x', state.range(0));
50  while (state.KeepRunning())
51    memcpy(dst, src, state.range(0));
52  state.SetBytesProcessed(int64_t(state.iterations()) *
53                          int64_t(state.range(0)));
54  delete[] src;
55  delete[] dst;
56}
57BENCHMARK(BM_memcpy)->Arg(8)->Arg(64)->Arg(512)->Arg(1<<10)->Arg(8<<10);
58```
59
60The preceding code is quite repetitive, and can be replaced with the following
61short-hand. The following invocation will pick a few appropriate arguments in
62the specified range and will generate a benchmark for each such argument.
63
64```c++
65BENCHMARK(BM_memcpy)->Range(8, 8<<10);
66```
67
68By default the arguments in the range are generated in multiples of eight and
69the command above selects [ 8, 64, 512, 4k, 8k ]. In the following code the
70range multiplier is changed to multiples of two.
71
72```c++
73BENCHMARK(BM_memcpy)->RangeMultiplier(2)->Range(8, 8<<10);
74```
75Now arguments generated are [ 8, 16, 32, 64, 128, 256, 512, 1024, 2k, 4k, 8k ].
76
77You might have a benchmark that depends on two or more inputs. For example, the
78following code defines a family of benchmarks for measuring the speed of set
79insertion.
80
81```c++
82static void BM_SetInsert(benchmark::State& state) {
83  while (state.KeepRunning()) {
84    state.PauseTiming();
85    std::set<int> data = ConstructRandomSet(state.range(0));
86    state.ResumeTiming();
87    for (int j = 0; j < state.range(1); ++j)
88      data.insert(RandomNumber());
89  }
90}
91BENCHMARK(BM_SetInsert)
92    ->Args({1<<10, 1})
93    ->Args({1<<10, 8})
94    ->Args({1<<10, 64})
95    ->Args({1<<10, 512})
96    ->Args({8<<10, 1})
97    ->Args({8<<10, 8})
98    ->Args({8<<10, 64})
99    ->Args({8<<10, 512});
100```
101
102The preceding code is quite repetitive, and can be replaced with the following
103short-hand. The following macro will pick a few appropriate arguments in the
104product of the two specified ranges and will generate a benchmark for each such
105pair.
106
107```c++
108BENCHMARK(BM_SetInsert)->Ranges({{1<<10, 8<<10}, {1, 512}});
109```
110
111For more complex patterns of inputs, passing a custom function to `Apply` allows
112programmatic specification of an arbitrary set of arguments on which to run the
113benchmark. The following example enumerates a dense range on one parameter,
114and a sparse range on the second.
115
116```c++
117static void CustomArguments(benchmark::internal::Benchmark* b) {
118  for (int i = 0; i <= 10; ++i)
119    for (int j = 32; j <= 1024*1024; j *= 8)
120      b->Args({i, j});
121}
122BENCHMARK(BM_SetInsert)->Apply(CustomArguments);
123```
124
125### Calculate asymptotic complexity (Big O)
126Asymptotic complexity might be calculated for a family of benchmarks. The
127following code will calculate the coefficient for the high-order term in the
128running time and the normalized root-mean square error of string comparison.
129
130```c++
131static void BM_StringCompare(benchmark::State& state) {
132  std::string s1(state.range(0), '-');
133  std::string s2(state.range(0), '-');
134  while (state.KeepRunning()) {
135    benchmark::DoNotOptimize(s1.compare(s2));
136  }
137  state.SetComplexityN(state.range(0));
138}
139BENCHMARK(BM_StringCompare)
140    ->RangeMultiplier(2)->Range(1<<10, 1<<18)->Complexity(benchmark::oN);
141```
142
143As shown in the following invocation, asymptotic complexity might also be
144calculated automatically.
145
146```c++
147BENCHMARK(BM_StringCompare)
148    ->RangeMultiplier(2)->Range(1<<10, 1<<18)->Complexity();
149```
150
151The following code will specify asymptotic complexity with a lambda function,
152that might be used to customize high-order term calculation.
153
154```c++
155BENCHMARK(BM_StringCompare)->RangeMultiplier(2)
156    ->Range(1<<10, 1<<18)->Complexity([](int n)->double{return n; });
157```
158
159### Templated benchmarks
160Templated benchmarks work the same way: This example produces and consumes
161messages of size `sizeof(v)` `range_x` times. It also outputs throughput in the
162absence of multiprogramming.
163
164```c++
165template <class Q> int BM_Sequential(benchmark::State& state) {
166  Q q;
167  typename Q::value_type v;
168  while (state.KeepRunning()) {
169    for (int i = state.range(0); i--; )
170      q.push(v);
171    for (int e = state.range(0); e--; )
172      q.Wait(&v);
173  }
174  // actually messages, not bytes:
175  state.SetBytesProcessed(
176      static_cast<int64_t>(state.iterations())*state.range(0));
177}
178BENCHMARK_TEMPLATE(BM_Sequential, WaitQueue<int>)->Range(1<<0, 1<<10);
179```
180
181Three macros are provided for adding benchmark templates.
182
183```c++
184#if __cplusplus >= 201103L // C++11 and greater.
185#define BENCHMARK_TEMPLATE(func, ...) // Takes any number of parameters.
186#else // C++ < C++11
187#define BENCHMARK_TEMPLATE(func, arg1)
188#endif
189#define BENCHMARK_TEMPLATE1(func, arg1)
190#define BENCHMARK_TEMPLATE2(func, arg1, arg2)
191```
192
193## Passing arbitrary arguments to a benchmark
194In C++11 it is possible to define a benchmark that takes an arbitrary number
195of extra arguments. The `BENCHMARK_CAPTURE(func, test_case_name, ...args)`
196macro creates a benchmark that invokes `func`  with the `benchmark::State` as
197the first argument followed by the specified `args...`.
198The `test_case_name` is appended to the name of the benchmark and
199should describe the values passed.
200
201```c++
202template <class ...ExtraArgs>`
203void BM_takes_args(benchmark::State& state, ExtraArgs&&... extra_args) {
204  [...]
205}
206// Registers a benchmark named "BM_takes_args/int_string_test` that passes
207// the specified values to `extra_args`.
208BENCHMARK_CAPTURE(BM_takes_args, int_string_test, 42, std::string("abc"));
209```
210Note that elements of `...args` may refer to global variables. Users should
211avoid modifying global state inside of a benchmark.
212
213## Using RegisterBenchmark(name, fn, args...)
214
215The `RegisterBenchmark(name, func, args...)` function provides an alternative
216way to create and register benchmarks.
217`RegisterBenchmark(name, func, args...)` creates, registers, and returns a
218pointer to a new benchmark with the specified `name` that invokes
219`func(st, args...)` where `st` is a `benchmark::State` object.
220
221Unlike the `BENCHMARK` registration macros, which can only be used at the global
222scope, the `RegisterBenchmark` can be called anywhere. This allows for
223benchmark tests to be registered programmatically.
224
225Additionally `RegisterBenchmark` allows any callable object to be registered
226as a benchmark. Including capturing lambdas and function objects. This
227allows the creation
228
229For Example:
230```c++
231auto BM_test = [](benchmark::State& st, auto Inputs) { /* ... */ };
232
233int main(int argc, char** argv) {
234  for (auto& test_input : { /* ... */ })
235      benchmark::RegisterBenchmark(test_input.name(), BM_test, test_input);
236  benchmark::Initialize(&argc, argv);
237  benchmark::RunSpecifiedBenchmarks();
238}
239```
240
241### Multithreaded benchmarks
242In a multithreaded test (benchmark invoked by multiple threads simultaneously),
243it is guaranteed that none of the threads will start until all have called
244`KeepRunning`, and all will have finished before KeepRunning returns false. As
245such, any global setup or teardown can be wrapped in a check against the thread
246index:
247
248```c++
249static void BM_MultiThreaded(benchmark::State& state) {
250  if (state.thread_index == 0) {
251    // Setup code here.
252  }
253  while (state.KeepRunning()) {
254    // Run the test as normal.
255  }
256  if (state.thread_index == 0) {
257    // Teardown code here.
258  }
259}
260BENCHMARK(BM_MultiThreaded)->Threads(2);
261```
262
263If the benchmarked code itself uses threads and you want to compare it to
264single-threaded code, you may want to use real-time ("wallclock") measurements
265for latency comparisons:
266
267```c++
268BENCHMARK(BM_test)->Range(8, 8<<10)->UseRealTime();
269```
270
271Without `UseRealTime`, CPU time is used by default.
272
273
274## Manual timing
275For benchmarking something for which neither CPU time nor real-time are
276correct or accurate enough, completely manual timing is supported using
277the `UseManualTime` function.
278
279When `UseManualTime` is used, the benchmarked code must call
280`SetIterationTime` once per iteration of the `KeepRunning` loop to
281report the manually measured time.
282
283An example use case for this is benchmarking GPU execution (e.g. OpenCL
284or CUDA kernels, OpenGL or Vulkan or Direct3D draw calls), which cannot
285be accurately measured using CPU time or real-time. Instead, they can be
286measured accurately using a dedicated API, and these measurement results
287can be reported back with `SetIterationTime`.
288
289```c++
290static void BM_ManualTiming(benchmark::State& state) {
291  int microseconds = state.range(0);
292  std::chrono::duration<double, std::micro> sleep_duration {
293    static_cast<double>(microseconds)
294  };
295
296  while (state.KeepRunning()) {
297    auto start = std::chrono::high_resolution_clock::now();
298    // Simulate some useful workload with a sleep
299    std::this_thread::sleep_for(sleep_duration);
300    auto end   = std::chrono::high_resolution_clock::now();
301
302    auto elapsed_seconds =
303      std::chrono::duration_cast<std::chrono::duration<double>>(
304        end - start);
305
306    state.SetIterationTime(elapsed_seconds.count());
307  }
308}
309BENCHMARK(BM_ManualTiming)->Range(1, 1<<17)->UseManualTime();
310```
311
312### Preventing optimisation
313To prevent a value or expression from being optimized away by the compiler
314the `benchmark::DoNotOptimize(...)` and `benchmark::ClobberMemory()`
315functions can be used.
316
317```c++
318static void BM_test(benchmark::State& state) {
319  while (state.KeepRunning()) {
320      int x = 0;
321      for (int i=0; i < 64; ++i) {
322        benchmark::DoNotOptimize(x += i);
323      }
324  }
325}
326```
327
328`DoNotOptimize(<expr>)` forces the  *result* of `<expr>` to be stored in either
329memory or a register. For GNU based compilers it acts as read/write barrier
330for global memory. More specifically it forces the compiler to flush pending
331writes to memory and reload any other values as necessary.
332
333Note that `DoNotOptimize(<expr>)` does not prevent optimizations on `<expr>`
334in any way. `<expr>` may even be removed entirely when the result is already
335known. For example:
336
337```c++
338  /* Example 1: `<expr>` is removed entirely. */
339  int foo(int x) { return x + 42; }
340  while (...) DoNotOptimize(foo(0)); // Optimized to DoNotOptimize(42);
341
342  /*  Example 2: Result of '<expr>' is only reused */
343  int bar(int) __attribute__((const));
344  while (...) DoNotOptimize(bar(0)); // Optimized to:
345  // int __result__ = bar(0);
346  // while (...) DoNotOptimize(__result__);
347```
348
349The second tool for preventing optimizations is `ClobberMemory()`. In essence
350`ClobberMemory()` forces the compiler to perform all pending writes to global
351memory. Memory managed by block scope objects must be "escaped" using
352`DoNotOptimize(...)` before it can be clobbered. In the below example
353`ClobberMemory()` prevents the call to `v.push_back(42)` from being optimized
354away.
355
356```c++
357static void BM_vector_push_back(benchmark::State& state) {
358  while (state.KeepRunning()) {
359    std::vector<int> v;
360    v.reserve(1);
361    benchmark::DoNotOptimize(v.data()); // Allow v.data() to be clobbered.
362    v.push_back(42);
363    benchmark::ClobberMemory(); // Force 42 to be written to memory.
364  }
365}
366```
367
368Note that `ClobberMemory()` is only available for GNU or MSVC based compilers.
369
370### Set time unit manually
371If a benchmark runs a few milliseconds it may be hard to visually compare the
372measured times, since the output data is given in nanoseconds per default. In
373order to manually set the time unit, you can specify it manually:
374
375```c++
376BENCHMARK(BM_test)->Unit(benchmark::kMillisecond);
377```
378
379## Controlling number of iterations
380In all cases, the number of iterations for which the benchmark is run is
381governed by the amount of time the benchmark takes. Concretely, the number of
382iterations is at least one, not more than 1e9, until CPU time is greater than
383the minimum time, or the wallclock time is 5x minimum time. The minimum time is
384set as a flag `--benchmark_min_time` or per benchmark by calling `MinTime` on
385the registered benchmark object.
386
387## Reporting the mean and standard devation by repeated benchmarks
388By default each benchmark is run once and that single result is reported.
389However benchmarks are often noisy and a single result may not be representative
390of the overall behavior. For this reason it's possible to repeatedly rerun the
391benchmark.
392
393The number of runs of each benchmark is specified globally by the
394`--benchmark_repetitions` flag or on a per benchmark basis by calling
395`Repetitions` on the registered benchmark object. When a benchmark is run
396more than once the mean and standard deviation of the runs will be reported.
397
398Additionally the `--benchmark_report_aggregates_only={true|false}` flag or
399`ReportAggregatesOnly(bool)` function can be used to change how repeated tests
400are reported. By default the result of each repeated run is reported. When this
401option is 'true' only the mean and standard deviation of the runs is reported.
402Calling `ReportAggregatesOnly(bool)` on a registered benchmark object overrides
403the value of the flag for that benchmark.
404
405## Fixtures
406Fixture tests are created by
407first defining a type that derives from ::benchmark::Fixture and then
408creating/registering the tests using the following macros:
409
410* `BENCHMARK_F(ClassName, Method)`
411* `BENCHMARK_DEFINE_F(ClassName, Method)`
412* `BENCHMARK_REGISTER_F(ClassName, Method)`
413
414For Example:
415
416```c++
417class MyFixture : public benchmark::Fixture {};
418
419BENCHMARK_F(MyFixture, FooTest)(benchmark::State& st) {
420   while (st.KeepRunning()) {
421     ...
422  }
423}
424
425BENCHMARK_DEFINE_F(MyFixture, BarTest)(benchmark::State& st) {
426   while (st.KeepRunning()) {
427     ...
428  }
429}
430/* BarTest is NOT registered */
431BENCHMARK_REGISTER_F(MyFixture, BarTest)->Threads(2);
432/* BarTest is now registered */
433```
434
435
436## User-defined counters
437
438You can add your own counters with user-defined names. The example below
439will add columns "Foo", "Bar" and "Baz" in its output:
440
441```c++
442static void UserCountersExample1(benchmark::State& state) {
443  double numFoos = 0, numBars = 0, numBazs = 0;
444  while (state.KeepRunning()) {
445    // ... count Foo,Bar,Baz events
446  }
447  state.counters["Foo"] = numFoos;
448  state.counters["Bar"] = numBars;
449  state.counters["Baz"] = numBazs;
450}
451```
452
453The `state.counters` object is a `std::map` with `std::string` keys
454and `Counter` values. The latter is a `double`-like class, via an implicit
455conversion to `double&`. Thus you can use all of the standard arithmetic
456assignment operators (`=,+=,-=,*=,/=`) to change the value of each counter.
457
458In multithreaded benchmarks, each counter is set on the calling thread only.
459When the benchmark finishes, the counters from each thread will be summed;
460the resulting sum is the value which will be shown for the benchmark.
461
462The `Counter` constructor accepts two parameters: the value as a `double`
463and a bit flag which allows you to show counters as rates and/or as
464per-thread averages:
465
466```c++
467  // sets a simple counter
468  state.counters["Foo"] = numFoos;
469
470  // Set the counter as a rate. It will be presented divided
471  // by the duration of the benchmark.
472  state.counters["FooRate"] = Counter(numFoos, benchmark::Counter::kIsRate);
473
474  // Set the counter as a thread-average quantity. It will
475  // be presented divided by the number of threads.
476  state.counters["FooAvg"] = Counter(numFoos, benchmark::Counter::kAvgThreads);
477
478  // There's also a combined flag:
479  state.counters["FooAvgRate"] = Counter(numFoos,benchmark::Counter::kAvgThreadsRate);
480```
481
482When you're compiling in C++11 mode or later you can use `insert()` with
483`std::initializer_list`:
484
485```c++
486  // With C++11, this can be done:
487  state.counters.insert({{"Foo", numFoos}, {"Bar", numBars}, {"Baz", numBazs}});
488  // ... instead of:
489  state.counters["Foo"] = numFoos;
490  state.counters["Bar"] = numBars;
491  state.counters["Baz"] = numBazs;
492```
493
494### Counter reporting
495
496When using the console reporter, by default, user counters are are printed at
497the end after the table, the same way as ``bytes_processed`` and
498``items_processed``. This is best for cases in which there are few counters,
499or where there are only a couple of lines per benchmark. Here's an example of
500the default output:
501
502```
503------------------------------------------------------------------------------
504Benchmark                        Time           CPU Iterations UserCounters...
505------------------------------------------------------------------------------
506BM_UserCounter/threads:8      2248 ns      10277 ns      68808 Bar=16 Bat=40 Baz=24 Foo=8
507BM_UserCounter/threads:1      9797 ns       9788 ns      71523 Bar=2 Bat=5 Baz=3 Foo=1024m
508BM_UserCounter/threads:2      4924 ns       9842 ns      71036 Bar=4 Bat=10 Baz=6 Foo=2
509BM_UserCounter/threads:4      2589 ns      10284 ns      68012 Bar=8 Bat=20 Baz=12 Foo=4
510BM_UserCounter/threads:8      2212 ns      10287 ns      68040 Bar=16 Bat=40 Baz=24 Foo=8
511BM_UserCounter/threads:16     1782 ns      10278 ns      68144 Bar=32 Bat=80 Baz=48 Foo=16
512BM_UserCounter/threads:32     1291 ns      10296 ns      68256 Bar=64 Bat=160 Baz=96 Foo=32
513BM_UserCounter/threads:4      2615 ns      10307 ns      68040 Bar=8 Bat=20 Baz=12 Foo=4
514BM_Factorial                    26 ns         26 ns   26608979 40320
515BM_Factorial/real_time          26 ns         26 ns   26587936 40320
516BM_CalculatePiRange/1           16 ns         16 ns   45704255 0
517BM_CalculatePiRange/8           73 ns         73 ns    9520927 3.28374
518BM_CalculatePiRange/64         609 ns        609 ns    1140647 3.15746
519BM_CalculatePiRange/512       4900 ns       4901 ns     142696 3.14355
520```
521
522If this doesn't suit you, you can print each counter as a table column by
523passing the flag `--benchmark_counters_tabular=true` to the benchmark
524application. This is best for cases in which there are a lot of counters, or
525a lot of lines per individual benchmark. Note that this will trigger a
526reprinting of the table header any time the counter set changes between
527individual benchmarks. Here's an example of corresponding output when
528`--benchmark_counters_tabular=true` is passed:
529
530```
531---------------------------------------------------------------------------------------
532Benchmark                        Time           CPU Iterations    Bar   Bat   Baz   Foo
533---------------------------------------------------------------------------------------
534BM_UserCounter/threads:8      2198 ns       9953 ns      70688     16    40    24     8
535BM_UserCounter/threads:1      9504 ns       9504 ns      73787      2     5     3     1
536BM_UserCounter/threads:2      4775 ns       9550 ns      72606      4    10     6     2
537BM_UserCounter/threads:4      2508 ns       9951 ns      70332      8    20    12     4
538BM_UserCounter/threads:8      2055 ns       9933 ns      70344     16    40    24     8
539BM_UserCounter/threads:16     1610 ns       9946 ns      70720     32    80    48    16
540BM_UserCounter/threads:32     1192 ns       9948 ns      70496     64   160    96    32
541BM_UserCounter/threads:4      2506 ns       9949 ns      70332      8    20    12     4
542--------------------------------------------------------------
543Benchmark                        Time           CPU Iterations
544--------------------------------------------------------------
545BM_Factorial                    26 ns         26 ns   26392245 40320
546BM_Factorial/real_time          26 ns         26 ns   26494107 40320
547BM_CalculatePiRange/1           15 ns         15 ns   45571597 0
548BM_CalculatePiRange/8           74 ns         74 ns    9450212 3.28374
549BM_CalculatePiRange/64         595 ns        595 ns    1173901 3.15746
550BM_CalculatePiRange/512       4752 ns       4752 ns     147380 3.14355
551BM_CalculatePiRange/4k       37970 ns      37972 ns      18453 3.14184
552BM_CalculatePiRange/32k     303733 ns     303744 ns       2305 3.14162
553BM_CalculatePiRange/256k   2434095 ns    2434186 ns        288 3.1416
554BM_CalculatePiRange/1024k  9721140 ns    9721413 ns         71 3.14159
555BM_CalculatePi/threads:8      2255 ns       9943 ns      70936
556```
557Note above the additional header printed when the benchmark changes from
558``BM_UserCounter`` to ``BM_Factorial``. This is because ``BM_Factorial`` does
559not have the same counter set as ``BM_UserCounter``.
560
561## Exiting Benchmarks in Error
562
563When errors caused by external influences, such as file I/O and network
564communication, occur within a benchmark the
565`State::SkipWithError(const char* msg)` function can be used to skip that run
566of benchmark and report the error. Note that only future iterations of the
567`KeepRunning()` are skipped. Users may explicitly return to exit the
568benchmark immediately.
569
570The `SkipWithError(...)` function may be used at any point within the benchmark,
571including before and after the `KeepRunning()` loop.
572
573For example:
574
575```c++
576static void BM_test(benchmark::State& state) {
577  auto resource = GetResource();
578  if (!resource.good()) {
579      state.SkipWithError("Resource is not good!");
580      // KeepRunning() loop will not be entered.
581  }
582  while (state.KeepRunning()) {
583      auto data = resource.read_data();
584      if (!resource.good()) {
585        state.SkipWithError("Failed to read data!");
586        break; // Needed to skip the rest of the iteration.
587     }
588     do_stuff(data);
589  }
590}
591```
592
593## Running a subset of the benchmarks
594
595The `--benchmark_filter=<regex>` option can be used to only run the benchmarks
596which match the specified `<regex>`. For example:
597
598```bash
599$ ./run_benchmarks.x --benchmark_filter=BM_memcpy/32
600Run on (1 X 2300 MHz CPU )
6012016-06-25 19:34:24
602Benchmark              Time           CPU Iterations
603----------------------------------------------------
604BM_memcpy/32          11 ns         11 ns   79545455
605BM_memcpy/32k       2181 ns       2185 ns     324074
606BM_memcpy/32          12 ns         12 ns   54687500
607BM_memcpy/32k       1834 ns       1837 ns     357143
608```
609
610
611## Output Formats
612The library supports multiple output formats. Use the
613`--benchmark_format=<console|json|csv>` flag to set the format type. `console`
614is the default format.
615
616The Console format is intended to be a human readable format. By default
617the format generates color output. Context is output on stderr and the
618tabular data on stdout. Example tabular output looks like:
619```
620Benchmark                               Time(ns)    CPU(ns) Iterations
621----------------------------------------------------------------------
622BM_SetInsert/1024/1                        28928      29349      23853  133.097kB/s   33.2742k items/s
623BM_SetInsert/1024/8                        32065      32913      21375  949.487kB/s   237.372k items/s
624BM_SetInsert/1024/10                       33157      33648      21431  1.13369MB/s   290.225k items/s
625```
626
627The JSON format outputs human readable json split into two top level attributes.
628The `context` attribute contains information about the run in general, including
629information about the CPU and the date.
630The `benchmarks` attribute contains a list of ever benchmark run. Example json
631output looks like:
632```json
633{
634  "context": {
635    "date": "2015/03/17-18:40:25",
636    "num_cpus": 40,
637    "mhz_per_cpu": 2801,
638    "cpu_scaling_enabled": false,
639    "build_type": "debug"
640  },
641  "benchmarks": [
642    {
643      "name": "BM_SetInsert/1024/1",
644      "iterations": 94877,
645      "real_time": 29275,
646      "cpu_time": 29836,
647      "bytes_per_second": 134066,
648      "items_per_second": 33516
649    },
650    {
651      "name": "BM_SetInsert/1024/8",
652      "iterations": 21609,
653      "real_time": 32317,
654      "cpu_time": 32429,
655      "bytes_per_second": 986770,
656      "items_per_second": 246693
657    },
658    {
659      "name": "BM_SetInsert/1024/10",
660      "iterations": 21393,
661      "real_time": 32724,
662      "cpu_time": 33355,
663      "bytes_per_second": 1199226,
664      "items_per_second": 299807
665    }
666  ]
667}
668```
669
670The CSV format outputs comma-separated values. The `context` is output on stderr
671and the CSV itself on stdout. Example CSV output looks like:
672```
673name,iterations,real_time,cpu_time,bytes_per_second,items_per_second,label
674"BM_SetInsert/1024/1",65465,17890.7,8407.45,475768,118942,
675"BM_SetInsert/1024/8",116606,18810.1,9766.64,3.27646e+06,819115,
676"BM_SetInsert/1024/10",106365,17238.4,8421.53,4.74973e+06,1.18743e+06,
677```
678
679## Output Files
680The library supports writing the output of the benchmark to a file specified
681by `--benchmark_out=<filename>`. The format of the output can be specified
682using `--benchmark_out_format={json|console|csv}`. Specifying
683`--benchmark_out` does not suppress the console output.
684
685## Debug vs Release
686By 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:
687
688```
689cmake -DCMAKE_BUILD_TYPE=Release
690```
691
692To enable link-time optimisation, use
693
694```
695cmake -DCMAKE_BUILD_TYPE=Release -DBENCHMARK_ENABLE_LTO=true
696```
697
698## Linking against the library
699When using gcc, it is necessary to link against pthread to avoid runtime exceptions.
700This is due to how gcc implements std::thread.
701See [issue #67](https://github.com/google/benchmark/issues/67) for more details.
702
703## Compiler Support
704
705Google Benchmark uses C++11 when building the library. As such we require
706a modern C++ toolchain, both compiler and standard library.
707
708The following minimum versions are strongly recommended build the library:
709
710* GCC 4.8
711* Clang 3.4
712* Visual Studio 2013
713* Intel 2015 Update 1
714
715Anything older *may* work.
716
717Note: Using the library and its headers in C++03 is supported. C++11 is only
718required to build the library.
719
720# Known Issues
721
722### Windows
723
724* Users must manually link `shlwapi.lib`. Failure to do so may result
725in unresolved symbols.
726
727

README.version

1URL: https://github.com/google/benchmark
2Version: 8da907c2c2786685c7da9f4759de052e3990f6f1
3BugComponent: 119451
4Owners: enh, android-bionic
5