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 <cstddef> |
13 | #include <deque> |
14 | #include <iterator> |
15 | #include <list> |
16 | #include <string> |
17 | #include <vector> |
18 | |
19 | #include "benchmark/benchmark.h" |
20 | #include "../../GenerateInput.h" |
21 | #include "test_macros.h" |
22 | |
23 | int main(int argc, char** argv) { |
24 | auto std_fill_n = [](auto out, auto n, auto const& value) { return std::fill_n(out, n, value); }; |
25 | |
26 | // {std,ranges}::fill_n(normal container) |
27 | { |
28 | auto bm = []<class Container>(std::string name, auto fill_n) { |
29 | benchmark::RegisterBenchmark( |
30 | name, |
31 | [fill_n](auto& st) { |
32 | std::size_t const size = st.range(0); |
33 | using ValueType = typename Container::value_type; |
34 | ValueType x = Generate<ValueType>::random(); |
35 | Container c(size, x); |
36 | |
37 | for ([[maybe_unused]] auto _ : st) { |
38 | benchmark::DoNotOptimize(c); |
39 | benchmark::DoNotOptimize(x); |
40 | fill_n(c.begin(), size, x); |
41 | benchmark::DoNotOptimize(c); |
42 | } |
43 | }) |
44 | ->Arg(32) |
45 | ->Arg(50) // non power-of-two |
46 | ->Arg(1024) |
47 | ->Arg(8192); |
48 | }; |
49 | bm.operator()<std::vector<int>>("std::fill_n(vector<int>)" , std_fill_n); |
50 | bm.operator()<std::deque<int>>("std::fill_n(deque<int>)" , std_fill_n); |
51 | bm.operator()<std::list<int>>("std::fill_n(list<int>)" , std_fill_n); |
52 | bm.operator()<std::vector<int>>("rng::fill_n(vector<int>)" , std::ranges::fill_n); |
53 | bm.operator()<std::deque<int>>("rng::fill_n(deque<int>)" , std::ranges::fill_n); |
54 | bm.operator()<std::list<int>>("rng::fill_n(list<int>)" , std::ranges::fill_n); |
55 | } |
56 | |
57 | // {std,ranges}::fill_n(vector<bool>) |
58 | { |
59 | auto bm = [](std::string name, auto fill_n) { |
60 | benchmark::RegisterBenchmark(name, [fill_n](auto& st) { |
61 | std::size_t const size = st.range(0); |
62 | bool x = true; |
63 | std::vector<bool> c(size, x); |
64 | |
65 | for ([[maybe_unused]] auto _ : st) { |
66 | benchmark::DoNotOptimize(c); |
67 | benchmark::DoNotOptimize(x); |
68 | fill_n(c.begin(), size, x); |
69 | benchmark::DoNotOptimize(c); |
70 | } |
71 | })->Range(64, 1 << 20); |
72 | }; |
73 | bm("std::fill_n(vector<bool>)" , std_fill_n); |
74 | #if TEST_STD_VER >= 23 // vector<bool>::iterator is not an output_iterator before C++23 |
75 | bm("rng::fill_n(vector<bool>)" , std::ranges::fill_n); |
76 | #endif |
77 | } |
78 | |
79 | benchmark::Initialize(&argc, argv); |
80 | benchmark::RunSpecifiedBenchmarks(); |
81 | benchmark::Shutdown(); |
82 | return 0; |
83 | } |
84 | |