From 1be36c8a50e1c2aae525c0fa95cb300ecf9abbe4 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Tue, 13 Sep 2022 23:27:41 -0700 Subject: Change `EndComparison` benchmark to not measure iteration. Also added `BM_Iteration` separately. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` BM_EndComparison 0.46ns ± 0% BM_Iteration/10/10 8.09ns ± 7% BM_Iteration/20/20 18.6ns ±16% BM_Iteration/100/100 79.0ns ±15% BM_Iteration/400/400 344ns ± 5% BM_Iteration/100/1 16.6ns ± 1% BM_Iteration/1000/10 454ns ± 3% ``` PiperOrigin-RevId: 474211728 Change-Id: I9bd799a4be3247ca8f2a2144b6e857db8c99c81f --- absl/container/internal/raw_hash_set_benchmark.cc | 42 +++++++++++++++++------ 1 file changed, 32 insertions(+), 10 deletions(-) (limited to 'absl/container/internal/raw_hash_set_benchmark.cc') diff --git a/absl/container/internal/raw_hash_set_benchmark.cc b/absl/container/internal/raw_hash_set_benchmark.cc index 47dc9048..fdec769b 100644 --- a/absl/container/internal/raw_hash_set_benchmark.cc +++ b/absl/container/internal/raw_hash_set_benchmark.cc @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "absl/container/internal/raw_hash_set.h" - #include #include +#include #include "absl/base/internal/raw_logging.h" #include "absl/container/internal/hash_function_defaults.h" +#include "absl/container/internal/raw_hash_set.h" #include "absl/strings/str_format.h" #include "benchmark/benchmark.h" @@ -202,23 +202,46 @@ void CacheInSteadyStateArgs(Benchmark* bm) { BENCHMARK(BM_CacheInSteadyState)->Apply(CacheInSteadyStateArgs); void BM_EndComparison(benchmark::State& state) { + StringTable t = {{"a", "a"}, {"b", "b"}}; + auto it = t.begin(); + for (auto i : state) { + benchmark::DoNotOptimize(t); + benchmark::DoNotOptimize(it); + benchmark::DoNotOptimize(it != t.end()); + } +} +BENCHMARK(BM_EndComparison); + +void BM_Iteration(benchmark::State& state) { std::random_device rd; std::mt19937 rng(rd()); string_generator gen{12}; StringTable t; - while (t.size() < state.range(0)) { + + size_t capacity = state.range(0); + size_t size = state.range(1); + t.reserve(capacity); + + while (t.size() < size) { t.emplace(gen(rng), gen(rng)); } - for (auto _ : state) { + for (auto i : state) { + benchmark::DoNotOptimize(t); for (auto it = t.begin(); it != t.end(); ++it) { - benchmark::DoNotOptimize(it); - benchmark::DoNotOptimize(t); - benchmark::DoNotOptimize(it != t.end()); + benchmark::DoNotOptimize(*it); } } } -BENCHMARK(BM_EndComparison)->Arg(400); + +BENCHMARK(BM_Iteration) + ->ArgPair(10, 10) + ->ArgPair(20, 20) + ->ArgPair(100, 100) + ->ArgPair(400, 400) + // sparse + ->ArgPair(100, 1) + ->ArgPair(1000, 10); void BM_CopyCtor(benchmark::State& state) { std::random_device rd; @@ -437,7 +460,6 @@ void CodegenAbslRawHashSetInt64Iterate( int odr = (::benchmark::DoNotOptimize(std::make_tuple( &CodegenAbslRawHashSetInt64Find, &CodegenAbslRawHashSetInt64FindNeEnd, - &CodegenAbslRawHashSetInt64Insert, - &CodegenAbslRawHashSetInt64Contains, + &CodegenAbslRawHashSetInt64Insert, &CodegenAbslRawHashSetInt64Contains, &CodegenAbslRawHashSetInt64Iterate)), 1); -- cgit v1.2.3