[libcxx-commits] [libcxx] [libc++] Rewrite the std::lower_bound benchmark to be more efficient and add an upper_bound benchmark (PR #177180)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jan 26 08:47:56 PST 2026


================
@@ -0,0 +1,86 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <deque>
+#include <forward_list>
+#include <list>
+#include <numeric>
+#include <random>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+#include "../../GenerateInput.h"
+
+int main(int argc, char** argv) {
+  {
+    auto bm = []<class Container>(benchmark::State& state) static {
+      std::mt19937_64 rng{123456};
+
+      using value_type = typename Container::value_type;
+
+      std::vector<value_type> vec;
+      for (int64_t i = 0; i != state.range(); ++i)
+        vec.emplace_back(Generate<typename Container::value_type>::random());
+      std::sort(vec.begin(), vec.end());
+
+      Container c(vec.begin(), vec.end());
+
+      for (auto _ : state) {
+        auto result = std::lower_bound(c.begin(), c.end(), vec[rng() % vec.size()]);
+        benchmark::DoNotOptimize(result);
+        benchmark::DoNotOptimize(vec);
+      }
+    };
+
+    auto register_benchmark = [&]<class Container>(std::type_identity<Container>, std::string name) {
+      benchmark::RegisterBenchmark(name, bm.template operator()<Container>)->Arg(8)->Arg(100)->Arg(8192);
+    };
+
+    register_benchmark(std::type_identity<std::vector<int>>{}, "std::lower_bound(std::vector<int>)");
+    register_benchmark(std::type_identity<std::deque<int>>{}, "std::lower_bound(std::deque<int>)");
+    register_benchmark(std::type_identity<std::list<int>>{}, "std::lower_bound(std::list<int>)");
+    register_benchmark(std::type_identity<std::forward_list<int>>{}, "std::lower_bound(std::forward_list<int>)");
+  }
+  {
+    auto bm = []<class Container>(benchmark::State& state) static {
----------------
ldionne wrote:

```suggestion
    auto bm = []<class Container>(benchmark::State& state) {
```

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


More information about the libcxx-commits mailing list