diff --git a/README.md b/README.md index 09ab18c6..6eb63f20 100644 --- a/README.md +++ b/README.md @@ -84,22 +84,23 @@ insertion. ```c++ static void BM_SetInsert(benchmark::State& state) { + std::set data; for (auto _ : state) { state.PauseTiming(); - std::set data = ConstructRandomSet(state.range(0)); + data = ConstructRandomSet(state.range(0)); state.ResumeTiming(); for (int j = 0; j < state.range(1); ++j) data.insert(RandomNumber()); } } BENCHMARK(BM_SetInsert) - ->Args({1<<10, 1}) - ->Args({1<<10, 8}) - ->Args({1<<10, 64}) + ->Args({1<<10, 128}) + ->Args({2<<10, 128}) + ->Args({4<<10, 128}) + ->Args({8<<10, 128}) ->Args({1<<10, 512}) - ->Args({8<<10, 1}) - ->Args({8<<10, 8}) - ->Args({8<<10, 64}) + ->Args({2<<10, 512}) + ->Args({4<<10, 512}) ->Args({8<<10, 512}); ``` @@ -109,7 +110,7 @@ product of the two specified ranges and will generate a benchmark for each such pair. ```c++ -BENCHMARK(BM_SetInsert)->Ranges({{1<<10, 8<<10}, {1, 512}}); +BENCHMARK(BM_SetInsert)->Ranges({{1<<10, 8<<10}, {128, 512}}); ``` For more complex patterns of inputs, passing a custom function to `Apply` allows diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h index 357e2424..d529e4bf 100644 --- a/include/benchmark/benchmark.h +++ b/include/benchmark/benchmark.h @@ -72,29 +72,30 @@ BENCHMARK(BM_memcpy)->Range(8, 8<<10); // example, the following code defines a family of microbenchmarks for // measuring the speed of set insertion. static void BM_SetInsert(benchmark::State& state) { + set data; for (auto _ : state) { state.PauseTiming(); - set data = ConstructRandomSet(state.range(0)); + data = ConstructRandomSet(state.range(0)); state.ResumeTiming(); for (int j = 0; j < state.range(1); ++j) data.insert(RandomNumber()); } } BENCHMARK(BM_SetInsert) - ->Args({1<<10, 1}) - ->Args({1<<10, 8}) - ->Args({1<<10, 64}) + ->Args({1<<10, 128}) + ->Args({2<<10, 128}) + ->Args({4<<10, 128}) + ->Args({8<<10, 128}) ->Args({1<<10, 512}) - ->Args({8<<10, 1}) - ->Args({8<<10, 8}) - ->Args({8<<10, 64}) + ->Args({2<<10, 512}) + ->Args({4<<10, 512}) ->Args({8<<10, 512}); // The preceding code is quite repetitive, and can be replaced with // the following short-hand. The following macro will pick a few // appropriate arguments in the product of the two specified ranges // and will generate a microbenchmark for each such pair. -BENCHMARK(BM_SetInsert)->Ranges({{1<<10, 8<<10}, {1, 512}}); +BENCHMARK(BM_SetInsert)->Ranges({{1<<10, 8<<10}, {128, 512}}); // For more complex patterns of inputs, passing a custom function // to Apply allows programmatic specification of an diff --git a/test/benchmark_test.cc b/test/benchmark_test.cc index 39590816..d4326012 100644 --- a/test/benchmark_test.cc +++ b/test/benchmark_test.cc @@ -42,7 +42,7 @@ double CalculatePi(int depth) { std::set ConstructRandomSet(int size) { std::set s; - for (int i = 0; i < size; ++i) s.insert(i); + for (int i = 0; i < size; ++i) s.insert(s.end(), i); return s; } @@ -82,16 +82,20 @@ BENCHMARK(BM_CalculatePi)->ThreadRange(1, 32); BENCHMARK(BM_CalculatePi)->ThreadPerCpu(); static void BM_SetInsert(benchmark::State& state) { + std::set data; for (auto _ : state) { state.PauseTiming(); - std::set data = ConstructRandomSet(state.range(0)); + data = ConstructRandomSet(state.range(0)); state.ResumeTiming(); for (int j = 0; j < state.range(1); ++j) data.insert(rand()); } state.SetItemsProcessed(state.iterations() * state.range(1)); state.SetBytesProcessed(state.iterations() * state.range(1) * sizeof(int)); } -BENCHMARK(BM_SetInsert)->Ranges({{1 << 10, 8 << 10}, {1, 10}}); + +// Test many inserts at once to reduce the total iterations needed. Otherwise, the slower, +// non-timed part of each iteration will make the benchmark take forever. +BENCHMARK(BM_SetInsert)->Ranges({{1 << 10, 8 << 10}, {128, 512}}); template diff --git a/test/map_test.cc b/test/map_test.cc index e7a26de1..0f8238d9 100644 --- a/test/map_test.cc +++ b/test/map_test.cc @@ -18,9 +18,10 @@ std::map ConstructRandomMap(int size) { // Basic version. static void BM_MapLookup(benchmark::State& state) { const int size = state.range(0); + std::map m; for (auto _ : state) { state.PauseTiming(); - std::map m = ConstructRandomMap(size); + m = ConstructRandomMap(size); state.ResumeTiming(); for (int i = 0; i < size; ++i) { benchmark::DoNotOptimize(m.find(rand() % size));