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#include <algorithm>
10#include <benchmark/benchmark.h>
11#include <vector>
12
13static void bm_fill_n(benchmark::State& state) {
14 std::vector<bool> vec1(state.range());
15 for (auto _ : state) {
16 benchmark::DoNotOptimize(value&: vec1);
17 benchmark::DoNotOptimize(value: std::fill_n(first: vec1.begin(), n: vec1.size(), value: false));
18 }
19}
20BENCHMARK(bm_fill_n)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
21
22static void bm_ranges_fill_n(benchmark::State& state) {
23 std::vector<bool> vec1(state.range());
24 for (auto _ : state) {
25 benchmark::DoNotOptimize(value&: vec1);
26 benchmark::DoNotOptimize(std::ranges::fill_n(vec1.begin(), vec1.size(), false));
27 }
28}
29BENCHMARK(bm_ranges_fill_n)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
30
31static void bm_fill(benchmark::State& state) {
32 std::vector<bool> vec1(state.range());
33 for (auto _ : state) {
34 benchmark::DoNotOptimize(value&: vec1);
35 std::fill(first: vec1.begin(), last: vec1.end(), value: false);
36 }
37}
38BENCHMARK(bm_fill)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
39
40static void bm_ranges_fill(benchmark::State& state) {
41 std::vector<bool> vec1(state.range());
42 for (auto _ : state) {
43 benchmark::DoNotOptimize(value&: vec1);
44 benchmark::DoNotOptimize(std::ranges::fill(vec1, false));
45 }
46}
47BENCHMARK(bm_ranges_fill)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
48
49BENCHMARK_MAIN();
50

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