aboutsummaryrefslogtreecommitdiff
path: root/libcxx/test/benchmarks/algorithms/modifying/fill_n.bench.cpp
blob: 8ad3e7534b1efa003589b7c9694e4b1cc513364a (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
//===----------------------------------------------------------------------===//
//
// 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"
#include "test_macros.h"

int main(int argc, char** argv) {
  auto std_fill_n = [](auto out, auto n, auto const& value) { return std::fill_n(out, n, value); };

  // {std,ranges}::fill_n(normal container)
  {
    auto bm = []<class Container>(std::string name, auto fill_n) {
      benchmark::RegisterBenchmark(
          name,
          [fill_n](auto& st) {
            std::size_t const size = st.range(0);
            using ValueType        = typename Container::value_type;
            ValueType x            = Generate<ValueType>::random();
            Container c(size, x);

            for ([[maybe_unused]] auto _ : st) {
              benchmark::DoNotOptimize(c);
              benchmark::DoNotOptimize(x);
              fill_n(c.begin(), size, x);
              benchmark::DoNotOptimize(c);
            }
          })
          ->Arg(32)
          ->Arg(50) // non power-of-two
          ->Arg(1024)
          ->Arg(8192);
    };
    bm.operator()<std::vector<int>>("std::fill_n(vector<int>)", std_fill_n);
    bm.operator()<std::deque<int>>("std::fill_n(deque<int>)", std_fill_n);
    bm.operator()<std::list<int>>("std::fill_n(list<int>)", std_fill_n);
    bm.operator()<std::vector<int>>("rng::fill_n(vector<int>)", std::ranges::fill_n);
    bm.operator()<std::deque<int>>("rng::fill_n(deque<int>)", std::ranges::fill_n);
    bm.operator()<std::list<int>>("rng::fill_n(list<int>)", std::ranges::fill_n);
  }

  // {std,ranges}::fill_n(vector<bool>)
  {
    auto bm = [](std::string name, auto fill_n) {
      benchmark::RegisterBenchmark(name, [fill_n](auto& st) {
        std::size_t const size = st.range(0);
        bool x                 = true;
        std::vector<bool> c(size, x);

        for ([[maybe_unused]] auto _ : st) {
          benchmark::DoNotOptimize(c);
          benchmark::DoNotOptimize(x);
          fill_n(c.begin(), size, x);
          benchmark::DoNotOptimize(c);
        }
      })->Range(64, 1 << 20);
    };
    bm("std::fill_n(vector<bool>)", std_fill_n);
#if TEST_STD_VER >= 23 // vector<bool>::iterator is not an output_iterator before C++23
    bm("rng::fill_n(vector<bool>)", std::ranges::fill_n);
#endif
  }

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