aboutsummaryrefslogtreecommitdiff
path: root/libcxx/test/benchmarks/algorithms/nonmodifying/is_permutation.bench.cpp
blob: 3e4d21945bf06e716d46596108b3d96b3193f450 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//===----------------------------------------------------------------------===//
//
// 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 <cstddef>
#include <deque>
#include <iterator>
#include <list>
#include <string>
#include <vector>

#include <benchmark/benchmark.h>
#include "../../GenerateInput.h"

int main(int argc, char** argv) {
  auto std_is_permutation_3leg = [](auto first1, auto last1, auto first2, auto) {
    return std::is_permutation(first1, last1, first2);
  };
  auto std_is_permutation_4leg = [](auto first1, auto last1, auto first2, auto last2) {
    return std::is_permutation(first1, last1, first2, last2);
  };
  auto std_is_permutation_3leg_pred = [](auto first1, auto last1, auto first2, auto) {
    return std::is_permutation(first1, last1, first2, [](auto x, auto y) {
      benchmark::DoNotOptimize(x);
      benchmark::DoNotOptimize(y);
      return x == y;
    });
  };
  auto std_is_permutation_4leg_pred = [](auto first1, auto last1, auto first2, auto last2) {
    return std::is_permutation(first1, last1, first2, last2, [](auto x, auto y) {
      benchmark::DoNotOptimize(x);
      benchmark::DoNotOptimize(y);
      return x == y;
    });
  };
  auto ranges_is_permutation_4leg_pred = [](auto first1, auto last1, auto first2, auto last2) {
    return std::ranges::is_permutation(first1, last1, first2, last2, [](auto x, auto y) {
      benchmark::DoNotOptimize(x);
      benchmark::DoNotOptimize(y);
      return x == y;
    });
  };

  auto register_benchmarks = [&](auto bm, std::string comment) {
    // std::is_permutation(it, it, it)
    bm.template operator()<std::vector<int>>(
        "std::is_permutation(vector<int>) (3leg) (" + comment + ")", std_is_permutation_3leg);
    bm.template operator()<std::deque<int>>(
        "std::is_permutation(deque<int>) (3leg) (" + comment + ")", std_is_permutation_3leg);
    bm.template operator()<std::list<int>>(
        "std::is_permutation(list<int>) (3leg) (" + comment + ")", std_is_permutation_3leg);

    // std::is_permutation(it, it, it, pred)
    bm.template operator()<std::vector<int>>(
        "std::is_permutation(vector<int>) (3leg, pred) (" + comment + ")", std_is_permutation_3leg_pred);
    bm.template operator()<std::deque<int>>(
        "std::is_permutation(deque<int>) (3leg, pred) (" + comment + ")", std_is_permutation_3leg_pred);
    bm.template operator()<std::list<int>>(
        "std::is_permutation(list<int>) (3leg, pred) (" + comment + ")", std_is_permutation_3leg_pred);

    // {std,ranges}::is_permutation(it, it, it, it)
    bm.template operator()<std::vector<int>>(
        "std::is_permutation(vector<int>) (4leg) (" + comment + ")", std_is_permutation_4leg);
    bm.template operator()<std::deque<int>>(
        "std::is_permutation(deque<int>) (4leg) (" + comment + ")", std_is_permutation_4leg);
    bm.template operator()<std::list<int>>(
        "std::is_permutation(list<int>) (4leg) (" + comment + ")", std_is_permutation_4leg);
    bm.template operator()<std::vector<int>>(
        "rng::is_permutation(vector<int>) (4leg) (" + comment + ")", std::ranges::is_permutation);
    bm.template operator()<std::deque<int>>(
        "rng::is_permutation(deque<int>) (4leg) (" + comment + ")", std::ranges::is_permutation);
    bm.template operator()<std::list<int>>(
        "rng::is_permutation(list<int>) (4leg) (" + comment + ")", std::ranges::is_permutation);

    // {std,ranges}::is_permutation(it, it, it, it, pred)
    bm.template operator()<std::vector<int>>(
        "std::is_permutation(vector<int>) (4leg, pred) (" + comment + ")", std_is_permutation_4leg_pred);
    bm.template operator()<std::deque<int>>(
        "std::is_permutation(deque<int>) (4leg, pred) (" + comment + ")", std_is_permutation_4leg_pred);
    bm.template operator()<std::list<int>>(
        "std::is_permutation(list<int>) (4leg, pred) (" + comment + ")", std_is_permutation_4leg_pred);
    bm.template operator()<std::vector<int>>(
        "rng::is_permutation(vector<int>) (4leg, pred) (" + comment + ")", ranges_is_permutation_4leg_pred);
    bm.template operator()<std::deque<int>>(
        "rng::is_permutation(deque<int>) (4leg, pred) (" + comment + ")", ranges_is_permutation_4leg_pred);
    bm.template operator()<std::list<int>>(
        "rng::is_permutation(list<int>) (4leg, pred) (" + comment + ")", ranges_is_permutation_4leg_pred);
  };

  // Benchmark {std,ranges}::is_permutation where both sequences share a common prefix (this can be optimized).
  {
    auto bm = []<class Container>(std::string name, auto is_permutation) {
      benchmark::RegisterBenchmark(
          name,
          [is_permutation](auto& st) {
            std::size_t const size = st.range(0);
            using ValueType        = typename Container::value_type;
            ValueType x            = Generate<ValueType>::random();
            ValueType y            = random_different_from({x});
            Container c1(size, x);
            Container c2(size, x);
            c2.back() = y;

            for ([[maybe_unused]] auto _ : st) {
              benchmark::DoNotOptimize(c1);
              benchmark::DoNotOptimize(c2);
              auto result = is_permutation(c1.begin(), c1.end(), c2.begin(), c2.end());
              benchmark::DoNotOptimize(result);
            }
          })
          ->Arg(8)
          ->Arg(1024)
          ->Arg(8192);
    };
    register_benchmarks(bm, "common prefix");
  }

  // Benchmark {std,ranges}::is_permutation on fully shuffled sequences.
  {
    auto bm = []<class Container>(std::string name, auto is_permutation) {
      benchmark::RegisterBenchmark(
          name,
          [is_permutation](auto& st) {
            std::size_t const size = st.range(0);
            using ValueType        = typename Container::value_type;
            std::vector<ValueType> data;
            std::generate_n(std::back_inserter(data), size, [] { return Generate<ValueType>::random(); });
            Container c1(data.begin(), data.end());

            std::mt19937 rng;
            std::shuffle(data.begin(), data.end(), rng);
            Container c2(data.begin(), data.end());

            for ([[maybe_unused]] auto _ : st) {
              benchmark::DoNotOptimize(c1);
              benchmark::DoNotOptimize(c2);
              auto result = is_permutation(c1.begin(), c1.end(), c2.begin(), c2.end());
              benchmark::DoNotOptimize(result);
            }
          })
          ->Arg(8)
          ->Arg(1024); // this one is very slow, no need for large sequences
    };
    register_benchmarks(bm, "shuffled");
  }

  benchmark::Initialize(&argc, argv);
  benchmark::RunSpecifiedBenchmarks();
  benchmark::Shutdown();
  return 0;
}