[libcxx-commits] [libcxx] [libc++] Optimize std::find_if (PR #167697)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Nov 13 11:07:42 PST 2025
================
@@ -0,0 +1,51 @@
+#include <benchmark/benchmark.h>
+
+template <class Iter, class ValueT>
+Iter my_find(Iter first, Iter last, const ValueT& i) {
+ for (; first != last; ++first) {
+ if (*first == i)
+ break;
+ }
+ return first;
+}
+
+static auto bm_find_if_no_vectorization(benchmark::State& state) {
+ std::size_t const size = 8192;
+ std::vector<short> c(size, 0);
+
+ for ([[maybe_unused]] auto _ : state) {
+ benchmark::DoNotOptimize(c);
+ std::vector<short>::iterator result;
+ [[clang::noinline]] result = my_find(c.begin(), c.end(), 1);
+ benchmark::DoNotOptimize(result);
+ }
+}
+BENCHMARK(bm_find_if_no_vectorization);
+
+static auto bm_find_if_autovectorization(benchmark::State& state) {
+ std::size_t const size = 8192;
+ std::vector<short> c(size, 0);
+
+ for ([[maybe_unused]] auto _ : state) {
+ benchmark::DoNotOptimize(c);
+ std::vector<short>::iterator result;
+ [[clang::noinline]] result = find_if(c.begin(), c.end(), [](short i) { return i == 1; });
----------------
ldionne wrote:
https://godbolt.org/z/qdda8WecM CC @fhahn
https://github.com/llvm/llvm-project/pull/167697
More information about the libcxx-commits
mailing list