-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
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 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. |
@llvm/pr-subscribers-libcxx Author: Imad Aldij (imdj) ChangesAdd benchmarks for is_permutation. Related to:
Full diff: https://github.com/llvm/llvm-project/pull/130387.diff 1 Files Affected:
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
|
Did you copy this from @ldionne's PR? |
No, I looked for benchmarks that target |
There was a problem hiding this 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.
My bad, I swear I scanned that PR earlier based on Mark's recommendation but I still somehow missed that |
Add benchmarks for is_permutation. Related to:
std::is_permutation
#129324