[libcxx-commits] [libcxx] [libc++] Add remaining benchmarks from [alg.modifying.operations] (PR #127354)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Mon Feb 24 13:22:34 PST 2025


================
@@ -0,0 +1,140 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <iterator>
+#include <list>
+#include <string>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+#include "../../GenerateInput.h"
+
+int main(int argc, char** argv) {
+  auto std_remove_if = [](auto first, auto last, auto pred) { return std::remove_if(first, last, pred); };
+
+  // Benchmark {std,ranges}::remove_if on a sequence of the form xxxxxxxxxxyyyyyyyyyy
+  // where we remove the prefix of x's from the sequence.
+  //
+  // We perform this benchmark in a batch because we need to restore the
+  // state of the container after the operation.
+  {
+    auto bm = []<class Container>(std::string name, auto remove_if) {
+      benchmark::RegisterBenchmark(
+          name,
+          [remove_if](auto& st) {
+            std::size_t const size          = st.range(0);
+            constexpr std::size_t BatchSize = 10;
+            using ValueType                 = typename Container::value_type;
+            Container c[BatchSize];
+            ValueType x = Generate<ValueType>::random();
+            ValueType y = random_different_from({x});
+            for (std::size_t i = 0; i != BatchSize; ++i) {
+              c[i]      = Container(size);
+              auto half = size / 2;
+              std::fill_n(std::fill_n(c[i].begin(), half, x), half, y);
+            }
+
+            auto pred = [&](auto& element) {
----------------
philnik777 wrote:

I think we want to take the argument by value to avoid requiring that the value is committed to memory everywhere.

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


More information about the libcxx-commits mailing list