[libcxx-commits] [libcxx] [libc++] Refactor and add benchmarks from [alg.nonmodifying] (PR #128206)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Mon Feb 24 14:44:04 PST 2025


================
@@ -0,0 +1,115 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+#include <algorithm>
+#include <cstddef>
+#include <deque>
+#include <initializer_list>
+#include <list>
+#include <string>
+#include <vector>
+
+#include <benchmark/benchmark.h>
+#include "../../GenerateInput.h"
+
+int main(int argc, char** argv) {
+  auto std_find_first_of = [](auto first1, auto last1, auto first2, auto last2) {
+    return std::find_first_of(first1, last1, first2, last2);
+  };
+
+  // Benchmark {std,ranges}::find_first_of where we have a hit at 25% of the haystack
+  // and at the end of the needle. This measures how quickly we're able to search inside
+  // the needle.
+  {
+    auto bm = []<class Container>(std::string name, auto find_first_of) {
+      benchmark::RegisterBenchmark(
+          name,
+          [find_first_of](auto& st) {
+            std::size_t const size = st.range(0);
+            using ValueType        = typename Container::value_type;
+            ValueType x            = Generate<ValueType>::random();
+            ValueType y            = random_different_from({x});
+            ValueType z            = random_different_from({x, y});
+            Container haystack(size, x);
+            Container needle(size, y);
+            needle.back() = z; // hit at the very end of the needle
+
+            // put the needle at 25% of the haystack
+            *std::next(haystack.begin(), haystack.size() / 4) = z;
+
+            for ([[maybe_unused]] auto _ : st) {
+              benchmark::DoNotOptimize(haystack);
+              benchmark::DoNotOptimize(needle);
+              auto result = find_first_of(haystack.begin(), haystack.end(), needle.begin(), needle.end());
+              benchmark::DoNotOptimize(result);
+            }
+          })
+          ->Arg(32)
+          ->Arg(1024)
+          ->Arg(8192);
+    };
+    bm.operator()<std::vector<int>>("std::find_first_of(vector<int>) (25% haystack, late needle)", std_find_first_of);
+    bm.operator()<std::deque<int>>("std::find_first_of(deque<int>) (25% haystack, late needle)", std_find_first_of);
+    bm.operator()<std::list<int>>("std::find_first_of(list<int>) (25% haystack, late needle)", std_find_first_of);
+    bm.operator()<std::vector<int>>(
+        "rng::find_first_of(vector<int>) (25% haystack, late needle)", std::ranges::find_first_of);
+    bm.operator()<std::deque<int>>(
+        "rng::find_first_of(deque<int>) (25% haystack, late needle)", std::ranges::find_first_of);
+    bm.operator()<std::list<int>>(
+        "rng::find_first_of(list<int>) (25% haystack, late needle)", std::ranges::find_first_of);
+  }
+
+  // Benchmark {std,ranges}::find_first_of where we have a hit at 90% of the haystack
+  // but at the beginning of the needle. This measures how quickly we're able to search
+  // inside the haystack.
+  {
----------------
philnik777 wrote:

We should add a benchmark with a small needle, since that is the really common case. We should also only keep the case where we find a big needle early in the haystack as a degenerate case. We don't really need a ton of coverage for degenerate cases.

https://github.com/llvm/llvm-project/pull/128206


More information about the libcxx-commits mailing list