[libcxx-commits] [libcxx] [libc++] Add benchmarks for partitioning algorithms (PR #127324)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Mon Feb 24 10:52:42 PST 2025


================
@@ -0,0 +1,74 @@
+//===----------------------------------------------------------------------===//
+//
+// 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"
+
+auto compute_median(auto first, auto last) {
+  std::vector v(first, last);
+  auto middle = v.begin() + v.size() / 2;
+  std::nth_element(v.begin(), middle, v.end());
+  return *middle;
+}
+
+int main(int argc, char** argv) {
+  auto std_partition_copy = [](auto first, auto last, auto out_yes, auto out_no, auto pred) {
+    return std::partition_copy(first, last, out_yes, out_no, pred);
+  };
+
+  auto bm = []<class Container>(std::string name, auto partition_copy) {
+    benchmark::RegisterBenchmark(
+        name,
+        [partition_copy](auto& st) {
+          std::size_t const size = st.range(0);
+          using ValueType        = typename Container::value_type;
+          Container c;
+          std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
+
+          std::vector<ValueType> yes(size), no(size);
----------------
philnik777 wrote:

Complete nit: I'm not a huge fan of defining multiple variables in one line.

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


More information about the libcxx-commits mailing list