Skip to content

[libc++] Add benchmarks for is_permutation and its std::ranges counte… #130387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

imdj
Copy link
Contributor

@imdj imdj commented Mar 8, 2025

@imdj imdj requested a review from a team as a code owner March 8, 2025 03:45
Copy link

github-actions bot commented Mar 8, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Mar 8, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 8, 2025

@llvm/pr-subscribers-libcxx

Author: Imad Aldij (imdj)

Changes

Add benchmarks for is_permutation. Related to:


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 --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

@mordante mordante self-assigned this Mar 8, 2025
@mordante
Copy link
Member

mordante commented Mar 8, 2025

Did you copy this from @ldionne's PR?

@imdj
Copy link
Contributor Author

imdj commented Mar 8, 2025

Did you copy this from @ldionne's PR?

No, I looked for benchmarks that target is_permutation but I couldn't find anything, maybe I missed it.
I just wanted to test my implementation so I made something basic by following the footsteps of other existing benchmarks (e.g. mismatch, ranges_end_with).

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch! Unfortunately it looks like we raced a bit here, I already had #128206 up which added similar benchmarks for {std,ranges}::is_permutation.

I had a look to make sure that both had similar coverage and I think that's the case. You do have different benchmarks for the following cases

  • both sequences are identical
  • both sequences differ only in the last element

However, I don't think the performance profile of the algorithm would change much between those two -- it would only change whether the answer is true or false. Therefore, I think the benchmarks for is_permutation I checked in recently probably supersede the ones added in this PR.

I am happy to work with you on getting #129565 reviewed, though.

@imdj
Copy link
Contributor Author

imdj commented Mar 19, 2025

My bad, I swear I scanned that PR earlier based on Mark's recommendation but I still somehow missed that is_permutation is covered in there.
Thank you for working on the benchmarks. I'll close this as redundant.

@imdj imdj closed this Mar 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants