[libcxx-commits] [libcxx] [libc++] Add benchmarks for is_permutation and its std::ranges counte… (PR #130387)
via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Mar 7 19:46:12 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Imad Aldij (imdj)
<details>
<summary>Changes</summary>
Add benchmarks for is_permutation. Related to:
- Issue: https://github.com/llvm/llvm-project/issues/129324
- Pull request: https://github.com/llvm/llvm-project/pull/129565
---
Full diff: https://github.com/llvm/llvm-project/pull/130387.diff
1 Files Affected:
- (added) libcxx/test/benchmarks/algorithms/is_permutation.bench.cpp (+112)
``````````diff
diff --git a/libcxx/test/benchmarks/algorithms/is_permutation.bench.cpp b/libcxx/test/benchmarks/algorithms/is_permutation.bench.cpp
new file mode 100644
index 0000000000000..d77896cb8ce6c
--- /dev/null
+++ b/libcxx/test/benchmarks/algorithms/is_permutation.bench.cpp
@@ -0,0 +1,112 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <random>
+#include <vector>
+#include <numeric>
+#include <benchmark/benchmark.h>
+
+void BenchmarkSizes(benchmark::internal::Benchmark* Benchmark) {
+ Benchmark->DenseRange(1, 8);
+ for (size_t i = 16; i != 1 << 20; i *= 2) {
+ Benchmark->Arg(i - 1);
+ Benchmark->Arg(i);
+ Benchmark->Arg(i + 1);
+ }
+}
+
+// Test std::is_permutation when sequences are identical
+static void bm_std_is_permutation_same(benchmark::State& state) {
+ std::vector<int> vec1(state.range(), 1);
+ std::vector<int> vec2(state.range(), 1);
+
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(vec1);
+ benchmark::DoNotOptimize(vec2);
+ benchmark::DoNotOptimize(std::is_permutation(vec1.begin(), vec1.end(), vec2.begin()));
+ }
+}
+BENCHMARK(bm_std_is_permutation_same)->Apply(BenchmarkSizes);
+
+// Test std::ranges::is_permutation when sequences are identical
+static void bm_ranges_is_permutation_same(benchmark::State& state) {
+ std::vector<int> vec1(state.range(), 1);
+ std::vector<int> vec2(state.range(), 1);
+
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(vec1);
+ benchmark::DoNotOptimize(vec2);
+ benchmark::DoNotOptimize(std::ranges::is_permutation(vec1, vec2));
+ }
+}
+BENCHMARK(bm_ranges_is_permutation_same)->Apply(BenchmarkSizes);
+
+// Test std::is_permutation when sequences are permutations
+static void bm_std_is_permutation_shuffled(benchmark::State& state) {
+ std::vector<int> vec1(state.range());
+ std::iota(vec1.begin(), vec1.end(), 0);
+ auto vec2 = vec1;
+ std::mt19937 gen(42);
+ std::shuffle(vec2.begin(), vec2.end(), gen);
+
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(vec1);
+ benchmark::DoNotOptimize(vec2);
+ benchmark::DoNotOptimize(std::is_permutation(vec1.begin(), vec1.end(), vec2.begin()));
+ }
+}
+BENCHMARK(bm_std_is_permutation_shuffled)->Apply(BenchmarkSizes);
+
+// Test std::ranges::is_permutation when sequences are permutations
+static void bm_ranges_is_permutation_shuffled(benchmark::State& state) {
+ std::vector<int> vec1(state.range());
+ std::iota(vec1.begin(), vec1.end(), 0);
+ auto vec2 = vec1;
+ std::mt19937 gen(42);
+ std::shuffle(vec2.begin(), vec2.end(), gen);
+
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(vec1);
+ benchmark::DoNotOptimize(vec2);
+ benchmark::DoNotOptimize(std::ranges::is_permutation(vec1, vec2));
+ }
+}
+BENCHMARK(bm_ranges_is_permutation_shuffled)->Apply(BenchmarkSizes);
+
+// Test std::is_permutation when sequences differ in last element
+static void bm_std_is_permutation_diff_last(benchmark::State& state) {
+ std::vector<int> vec1(state.range(), 1);
+ std::vector<int> vec2(state.range(), 1);
+ vec2.back() = 2;
+
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(vec1);
+ benchmark::DoNotOptimize(vec2);
+ benchmark::DoNotOptimize(std::is_permutation(vec1.begin(), vec1.end(), vec2.begin()));
+ }
+}
+BENCHMARK(bm_std_is_permutation_diff_last)->Apply(BenchmarkSizes);
+
+// Test std::ranges::is_permutation when sequences differ in last element
+static void bm_ranges_is_permutation_diff_last(benchmark::State& state) {
+ std::vector<int> vec1(state.range(), 1);
+ std::vector<int> vec2(state.range(), 1);
+ vec2.back() = 2;
+
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(vec1);
+ benchmark::DoNotOptimize(vec2);
+ benchmark::DoNotOptimize(std::ranges::is_permutation(vec1, vec2));
+ }
+}
+BENCHMARK(bm_ranges_is_permutation_diff_last)->Apply(BenchmarkSizes);
+
+BENCHMARK_MAIN();
\ No newline at end of file
``````````
</details>
https://github.com/llvm/llvm-project/pull/130387
More information about the libcxx-commits
mailing list