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
|
//===----------------------------------------------------------------------===//
//
// 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 <random>
#include <string>
#include <vector>
#include "benchmark/benchmark.h"
#include "../../GenerateInput.h"
int main(int argc, char** argv) {
auto std_sample = [](auto first, auto last, auto out, auto n, auto& rng) {
return std::sample(first, last, out, n, rng);
};
// {std,ranges}::sample(normal container)
{
auto bm = []<class Container>(std::string name, auto sample) {
benchmark::RegisterBenchmark(
name,
[sample](auto& st) {
std::size_t const size = st.range(0);
using ValueType = typename Container::value_type;
Container c;
std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
std::vector<ValueType> out(size);
auto const n = size / 4; // sample 1/4 of the range
std::mt19937 rng;
for ([[maybe_unused]] auto _ : st) {
benchmark::DoNotOptimize(c);
benchmark::DoNotOptimize(out);
auto result = sample(c.begin(), c.end(), out.begin(), n, rng);
benchmark::DoNotOptimize(result);
}
})
->Arg(32)
->Arg(1024)
->Arg(8192);
};
bm.operator()<std::vector<int>>("std::sample(vector<int>)", std_sample);
bm.operator()<std::deque<int>>("std::sample(deque<int>)", std_sample);
bm.operator()<std::list<int>>("std::sample(list<int>)", std_sample);
bm.operator()<std::vector<int>>("rng::sample(vector<int>)", std::ranges::sample);
bm.operator()<std::deque<int>>("rng::sample(deque<int>)", std::ranges::sample);
bm.operator()<std::list<int>>("rng::sample(list<int>)", std::ranges::sample);
}
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
return 0;
}
|