1+ // ===----------------------------------------------------------------------===//
2+ //
3+ // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+ // See https://llvm.org/LICENSE.txt for license information.
5+ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+ //
7+ // ===----------------------------------------------------------------------===//
8+
9+ // UNSUPPORTED: c++03, c++11, c++14, c++17
10+
11+ #include < algorithm>
12+ #include < random>
13+ #include < vector>
14+ #include < numeric>
15+ #include < benchmark/benchmark.h>
16+
17+ void BenchmarkSizes (benchmark::internal::Benchmark* Benchmark) {
18+ Benchmark->DenseRange (1 , 8 );
19+ for (size_t i = 16 ; i != 1 << 20 ; i *= 2 ) {
20+ Benchmark->Arg (i - 1 );
21+ Benchmark->Arg (i);
22+ Benchmark->Arg (i + 1 );
23+ }
24+ }
25+
26+ // Test std::is_permutation when sequences are identical
27+ static void bm_std_is_permutation_same (benchmark::State& state) {
28+ std::vector<int > vec1 (state.range (), 1 );
29+ std::vector<int > vec2 (state.range (), 1 );
30+
31+ for (auto _ : state) {
32+ benchmark::DoNotOptimize (vec1);
33+ benchmark::DoNotOptimize (vec2);
34+ benchmark::DoNotOptimize (std::is_permutation (vec1.begin (), vec1.end (), vec2.begin ()));
35+ }
36+ }
37+ BENCHMARK (bm_std_is_permutation_same)->Apply(BenchmarkSizes);
38+
39+ // Test std::ranges::is_permutation when sequences are identical
40+ static void bm_ranges_is_permutation_same (benchmark::State& state) {
41+ std::vector<int > vec1 (state.range (), 1 );
42+ std::vector<int > vec2 (state.range (), 1 );
43+
44+ for (auto _ : state) {
45+ benchmark::DoNotOptimize (vec1);
46+ benchmark::DoNotOptimize (vec2);
47+ benchmark::DoNotOptimize (std::ranges::is_permutation (vec1, vec2));
48+ }
49+ }
50+ BENCHMARK (bm_ranges_is_permutation_same)->Apply(BenchmarkSizes);
51+
52+ // Test std::is_permutation when sequences are permutations
53+ static void bm_std_is_permutation_shuffled (benchmark::State& state) {
54+ std::vector<int > vec1 (state.range ());
55+ std::iota (vec1.begin (), vec1.end (), 0 );
56+ auto vec2 = vec1;
57+ std::mt19937 gen (42 );
58+ std::shuffle (vec2.begin (), vec2.end (), gen);
59+
60+ for (auto _ : state) {
61+ benchmark::DoNotOptimize (vec1);
62+ benchmark::DoNotOptimize (vec2);
63+ benchmark::DoNotOptimize (std::is_permutation (vec1.begin (), vec1.end (), vec2.begin ()));
64+ }
65+ }
66+ BENCHMARK (bm_std_is_permutation_shuffled)->Apply(BenchmarkSizes);
67+
68+ // Test std::ranges::is_permutation when sequences are permutations
69+ static void bm_ranges_is_permutation_shuffled (benchmark::State& state) {
70+ std::vector<int > vec1 (state.range ());
71+ std::iota (vec1.begin (), vec1.end (), 0 );
72+ auto vec2 = vec1;
73+ std::mt19937 gen (42 );
74+ std::shuffle (vec2.begin (), vec2.end (), gen);
75+
76+ for (auto _ : state) {
77+ benchmark::DoNotOptimize (vec1);
78+ benchmark::DoNotOptimize (vec2);
79+ benchmark::DoNotOptimize (std::ranges::is_permutation (vec1, vec2));
80+ }
81+ }
82+ BENCHMARK (bm_ranges_is_permutation_shuffled)->Apply(BenchmarkSizes);
83+
84+ // Test std::is_permutation when sequences differ in last element
85+ static void bm_std_is_permutation_diff_last (benchmark::State& state) {
86+ std::vector<int > vec1 (state.range (), 1 );
87+ std::vector<int > vec2 (state.range (), 1 );
88+ vec2.back () = 2 ;
89+
90+ for (auto _ : state) {
91+ benchmark::DoNotOptimize (vec1);
92+ benchmark::DoNotOptimize (vec2);
93+ benchmark::DoNotOptimize (std::is_permutation (vec1.begin (), vec1.end (), vec2.begin ()));
94+ }
95+ }
96+ BENCHMARK (bm_std_is_permutation_diff_last)->Apply(BenchmarkSizes);
97+
98+ // Test std::ranges::is_permutation when sequences differ in last element
99+ static void bm_ranges_is_permutation_diff_last (benchmark::State& state) {
100+ std::vector<int > vec1 (state.range (), 1 );
101+ std::vector<int > vec2 (state.range (), 1 );
102+ vec2.back () = 2 ;
103+
104+ for (auto _ : state) {
105+ benchmark::DoNotOptimize (vec1);
106+ benchmark::DoNotOptimize (vec2);
107+ benchmark::DoNotOptimize (std::ranges::is_permutation (vec1, vec2));
108+ }
109+ }
110+ BENCHMARK (bm_ranges_is_permutation_diff_last)->Apply(BenchmarkSizes);
111+
112+ BENCHMARK_MAIN ();
0 commit comments