[libcxx-commits] [libcxx] fd78472 - [libc++] Rewrite minmax_element benchmark

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Sat Dec 21 11:08:46 PST 2024


Author: Nikolas Klauser
Date: 2024-12-21T20:07:28+01:00
New Revision: fd784726db70a5155594c32ee839b8807fafd87d

URL: https://github.com/llvm/llvm-project/commit/fd784726db70a5155594c32ee839b8807fafd87d
DIFF: https://github.com/llvm/llvm-project/commit/fd784726db70a5155594c32ee839b8807fafd87d.diff

LOG: [libc++] Rewrite minmax_element benchmark

The benchmark currently uses makeCartesianProductBenchmark, which
doesn't make a ton of sense, since minmax_element always goes through
every element one by one. The runtime doesn't depend on the values
of the elements.

Fixes #120758

Added: 
    

Modified: 
    libcxx/test/benchmarks/algorithms/min_max_element.bench.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/test/benchmarks/algorithms/min_max_element.bench.cpp b/libcxx/test/benchmarks/algorithms/min_max_element.bench.cpp
index 4dbfed66d0c2cf..dea48b826e3073 100644
--- a/libcxx/test/benchmarks/algorithms/min_max_element.bench.cpp
+++ b/libcxx/test/benchmarks/algorithms/min_max_element.bench.cpp
@@ -6,33 +6,42 @@
 //
 //===----------------------------------------------------------------------===//
 
-// UNSUPPORTED: c++03, c++11, c++14
+// UNSUPPORTED: c++03, c++11, c++14, c++17
 
 #include <algorithm>
+#include <vector>
 
-#include "common.h"
+#include <benchmark/benchmark.h>
 
-namespace {
-template <class ValueType, class Order>
-struct MinMaxElement {
-  size_t Quantity;
+void run_sizes(auto benchmark) {
+  benchmark->Arg(1)
+      ->Arg(2)
+      ->Arg(3)
+      ->Arg(4)
+      ->Arg(64)
+      ->Arg(512)
+      ->Arg(1024)
+      ->Arg(4000)
+      ->Arg(4096)
+      ->Arg(5500)
+      ->Arg(64000)
+      ->Arg(65536)
+      ->Arg(70000);
+}
 
-  void run(benchmark::State& state) const {
-    runOpOnCopies<ValueType>(state, Quantity, Order(), BatchSize::CountElements, [](auto& Copy) {
-      benchmark::DoNotOptimize(std::minmax_element(Copy.begin(), Copy.end()));
-    });
-  }
+template <class T>
+void BM_std_minmax_element(benchmark::State& state) {
+  std::vector<T> vec(state.range(), 3);
 
-  std::string name() const {
-    return "BM_MinMaxElement" + ValueType::name() + Order::name() + "_" + std::to_string(Quantity);
+  for (auto _ : state) {
+    benchmark::DoNotOptimize(vec);
+    benchmark::DoNotOptimize(std::minmax_element(vec.begin(), vec.end()));
   }
-};
-} // namespace
-
-int main(int argc, char** argv) {
-  benchmark::Initialize(&argc, argv);
-  if (benchmark::ReportUnrecognizedArguments(argc, argv))
-    return 1;
-  makeCartesianProductBenchmark<MinMaxElement, AllValueTypes, AllOrders>(Quantities);
-  benchmark::RunSpecifiedBenchmarks();
 }
+
+BENCHMARK(BM_std_minmax_element<char>)->Apply(run_sizes);
+BENCHMARK(BM_std_minmax_element<short>)->Apply(run_sizes);
+BENCHMARK(BM_std_minmax_element<int>)->Apply(run_sizes);
+BENCHMARK(BM_std_minmax_element<long long>)->Apply(run_sizes);
+
+BENCHMARK_MAIN();


        


More information about the libcxx-commits mailing list