[libcxx-commits] [libcxx] [libc++] Add benchmarks for std::reduce and std::transform_reduce (PR #233374)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Oct 21 09:49:00 PDT 2026
================
@@ -0,0 +1,72 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++20
+
+#include <algorithm>
+#include <cstddef>
+#include <iterator>
+#include <list>
+#include <numeric>
+#include <string>
+#include <type_traits>
+#include <vector>
+
+#include <benchmark/benchmark.h>
+#include "../GenerateInput.h"
+#include "test_macros.h"
+
+// Keep values small so that signed integer arithmetic does not overflow even for the largest inputs
+template <class T>
+T random_value() {
+ if constexpr (std::is_integral_v<T>)
+ return getRandomInteger<T>(-100, 100);
+ else
+ return getRandomFloat<T>(-100, 100);
+}
+
+int main(int argc, char** argv) {
+ // std::reduce(first, last, init, reduce)
+ {
+ auto bm = []<class Container>(std::string name) {
+ benchmark::RegisterBenchmark(
+ name,
+ [](auto& st) TEST_ALIGN_BENCHMARK {
+ 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 random_value<ValueType>(); });
+ ValueType init = random_value<ValueType>();
+
+ auto reduce = [](auto x, auto y) { return x + y; };
+
+ for ([[maybe_unused]] auto _ : st) {
----------------
ldionne wrote:
```suggestion
for (auto _ : st) {
```
I know we do this in a bunch of places, but I don't think it's actually requried.
https://github.com/llvm/llvm-project/pull/233374
More information about the libcxx-commits
mailing list