[libcxx-commits] [libcxx] b968e74 - [libc++] Add benchmarks for std::reduce and std::transform_reduce (#233374)
via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Oct 24 00:29:06 PDT 2026
Author: Michael G. Kazakov
Date: 2026-10-24T08:29:01+01:00
New Revision: b968e745fb9552b0da3b7fdfaf70c2c31eb7bece
URL: https://github.com/llvm/llvm-project/commit/b968e745fb9552b0da3b7fdfaf70c2c31eb7bece
DIFF: https://github.com/llvm/llvm-project/commit/b968e745fb9552b0da3b7fdfaf70c2c31eb7bece.diff
LOG: [libc++] Add benchmarks for std::reduce and std::transform_reduce (#233374)
The PR adds benchmarks for these algorithms:
- `std::reduce()`
- `std::transform_reduce()`, unary transform
- `std::transform_reduce()`, binary transform
The benchmarks run minimal reduction and transformation operators with
`int`, `float` and `double` parameters.
`std::vector` and `std::list` containers are tested, `std::deque` is not
included to reduce the number of added benchmarks.
Fixes #226211
---------
Co-authored-by: Louis Dionne <ldionne.2 at gmail.com>
Added:
libcxx/test/benchmarks/numeric/reduce.bench.cpp
libcxx/test/benchmarks/numeric/transform_reduce.bench.cpp
Modified:
Removed:
################################################################################
diff --git a/libcxx/test/benchmarks/numeric/reduce.bench.cpp b/libcxx/test/benchmarks/numeric/reduce.bench.cpp
new file mode 100644
index 00000000000000..ece7578d18ff32
--- /dev/null
+++ b/libcxx/test/benchmarks/numeric/reduce.bench.cpp
@@ -0,0 +1,80 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 multiplication doesn't overflow
+template <class T>
+T random_value() {
+ if constexpr (std::is_integral_v<T>)
+ return getRandomInteger<T>(1, 1000);
+ else
+ return getRandomFloat<T>(1, 1000);
+}
+
+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 = static_cast<ValueType>(1);
+
+ auto reduce = [](auto x, auto y) {
+ if constexpr (std::is_floating_point_v<decltype(x)>) {
+ // Compilers aren't allowed to optimize away the FP divisions and multiplications without fast-math.
+ return x / 3. * 3. / 5. * 5. + y / 7. * 7. / 9. * 9.;
+ } else {
+ // p=46337 is the largest prime with '(p-1)^2 <= INT_MAX', so the chain of reductions never overflows.
+ constexpr decltype(x) p = 46337;
+ return x * y % p;
+ }
+ };
+ for (auto _ : st) {
+ benchmark::ClobberMemory();
+ auto result = std::reduce(c.begin(), c.end(), init, reduce);
+ benchmark::DoNotOptimize(result);
+ }
+ })
+ ->Arg(32)
+ ->Arg(50) // non power-of-two
+ ->Arg(1024)
+ ->Arg(8192);
+ };
+ bm.operator()<std::vector<int>>("std::reduce(vector<int>)");
+ bm.operator()<std::vector<float>>("std::reduce(vector<float>)");
+ bm.operator()<std::vector<double>>("std::reduce(vector<double>)");
+ bm.operator()<std::list<int>>("std::reduce(list<int>)");
+ bm.operator()<std::list<float>>("std::reduce(list<float>)");
+ bm.operator()<std::list<double>>("std::reduce(list<double>)");
+ }
+
+ benchmark::Initialize(&argc, argv);
+ benchmark::RunSpecifiedBenchmarks();
+ benchmark::Shutdown();
+ return 0;
+}
diff --git a/libcxx/test/benchmarks/numeric/transform_reduce.bench.cpp b/libcxx/test/benchmarks/numeric/transform_reduce.bench.cpp
new file mode 100644
index 00000000000000..192308f1a9031d
--- /dev/null
+++ b/libcxx/test/benchmarks/numeric/transform_reduce.bench.cpp
@@ -0,0 +1,127 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 multiplication doesn't overflow
+template <class T>
+T random_value() {
+ if constexpr (std::is_integral_v<T>)
+ return getRandomInteger<T>(1, 1000);
+ else
+ return getRandomFloat<T>(1, 1000);
+}
+
+int main(int argc, char** argv) {
+ // std::transform_reduce(first, last, init, reduce, transform)
+ {
+ 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 = static_cast<ValueType>(1);
+
+ auto transform = [](auto x) { return x * 3; };
+ auto reduce = [](auto x, auto y) {
+ if constexpr (std::is_floating_point_v<decltype(x)>) {
+ // Compilers aren't allowed to optimize away the FP divisions and multiplications without fast-math.
+ return x / 3. * 3. / 5. * 5. + y / 7. * 7. / 9. * 9.;
+ } else {
+ // p=46337 is the largest prime with '(p-1)^2 <= INT_MAX', so the chain of reductions never overflows.
+ constexpr decltype(x) p = 46337;
+ return x * y % p;
+ }
+ };
+
+ for ([[maybe_unused]] auto _ : st) {
+ benchmark::ClobberMemory();
+ auto result = std::transform_reduce(c.begin(), c.end(), init, reduce, transform);
+ benchmark::DoNotOptimize(result);
+ }
+ })
+ ->Arg(32)
+ ->Arg(50) // non power-of-two
+ ->Arg(1024)
+ ->Arg(8192);
+ };
+ bm.operator()<std::vector<int>>("std::transform_reduce(vector<int>)");
+ bm.operator()<std::vector<float>>("std::transform_reduce(vector<float>)");
+ bm.operator()<std::vector<double>>("std::transform_reduce(vector<double>)");
+ bm.operator()<std::list<int>>("std::transform_reduce(list<int>)");
+ bm.operator()<std::list<float>>("std::transform_reduce(list<float>)");
+ bm.operator()<std::list<double>>("std::transform_reduce(list<double>)");
+ }
+
+ // std::transform_reduce(first1, last1, first2, init, reduce, transform)
+ {
+ 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 c1, c2;
+ std::generate_n(std::back_inserter(c1), size, [] { return random_value<ValueType>(); });
+ std::generate_n(std::back_inserter(c2), size, [] { return random_value<ValueType>(); });
+ ValueType init = static_cast<ValueType>(1);
+
+ auto transform = [](auto x, auto y) { return x - y; };
+ auto reduce = [](auto x, auto y) {
+ if constexpr (std::is_floating_point_v<decltype(x)>) {
+ // Compilers aren't allowed to optimize away the FP divisions and multiplications without fast-math.
+ return x / 3. * 3. / 5. * 5. + y / 7. * 7. / 9. * 9.;
+ } else {
+ // p=46337 is the largest prime with '(p-1)^2 <= INT_MAX', so the chain of reductions never overflows.
+ constexpr decltype(x) p = 46337;
+ return x * y % p;
+ }
+ };
+
+ for ([[maybe_unused]] auto _ : st) {
+ benchmark::ClobberMemory();
+ auto result = std::transform_reduce(c1.begin(), c1.end(), c2.begin(), init, reduce, transform);
+ benchmark::DoNotOptimize(result);
+ }
+ })
+ ->Arg(32)
+ ->Arg(50) // non power-of-two
+ ->Arg(1024)
+ ->Arg(8192);
+ };
+ bm.operator()<std::vector<int>>("std::transform_reduce(vector<int>, vector<int>)");
+ bm.operator()<std::vector<float>>("std::transform_reduce(vector<float>, vector<float>)");
+ bm.operator()<std::vector<double>>("std::transform_reduce(vector<double>, vector<double>)");
+ bm.operator()<std::list<int>>("std::transform_reduce(list<int>, list<int>)");
+ bm.operator()<std::list<float>>("std::transform_reduce(list<float>, list<float>)");
+ bm.operator()<std::list<double>>("std::transform_reduce(list<double>, list<double>)");
+ }
+
+ benchmark::Initialize(&argc, argv);
+ benchmark::RunSpecifiedBenchmarks();
+ benchmark::Shutdown();
+ return 0;
+}
More information about the libcxx-commits
mailing list