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
23int main(int argc, char** argv) {
24 auto std_fill = [](auto first, auto last, auto const& value) { return std::fill(first, last, value); };
25
26 // {std,ranges}::fill(normal container)
27 {
28 auto bm = []<class Container>(std::string name, auto fill) {
29 benchmark::RegisterBenchmark(
30 name,
31 [fill](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(c.begin(), c.end(), 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(vector<int>)", std_fill);
50 bm.operator()<std::deque<int>>("std::fill(deque<int>)", std_fill);
51 bm.operator()<std::list<int>>("std::fill(list<int>)", std_fill);
52 bm.operator()<std::vector<int>>("rng::fill(vector<int>)", std::ranges::fill);
53 bm.operator()<std::deque<int>>("rng::fill(deque<int>)", std::ranges::fill);
54 bm.operator()<std::list<int>>("rng::fill(list<int>)", std::ranges::fill);
55 }
56
57 // {std,ranges}::fill(vector<bool>)
58 {
59 auto bm = [](std::string name, auto fill) {
60 benchmark::RegisterBenchmark(name, [fill](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(c.begin(), c.end(), x);
69 benchmark::DoNotOptimize(c);
70 }
71 })->Range(64, 1 << 20);
72 };
73 bm("std::fill(vector<bool>)", std_fill);
74#if TEST_STD_VER >= 23 // vector<bool>::iterator is not an output_iterator before C++23
75 bm("rng::fill(vector<bool>)", std::ranges::fill);
76#endif
77 }
78
79 benchmark::Initialize(&argc, argv);
80 benchmark::RunSpecifiedBenchmarks();
81 benchmark::Shutdown();
82 return 0;
83}
84

source code of libcxx/test/benchmarks/algorithms/modifying/fill.bench.cpp