//===----------------------------------------------------------------------===// // // 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 #include #include #include #include #include #include #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 = [](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::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::fill_n(vector)", std_fill_n); bm.operator()>("std::fill_n(deque)", std_fill_n); bm.operator()>("std::fill_n(list)", std_fill_n); bm.operator()>("rng::fill_n(vector)", std::ranges::fill_n); bm.operator()>("rng::fill_n(deque)", std::ranges::fill_n); bm.operator()>("rng::fill_n(list)", std::ranges::fill_n); } // {std,ranges}::fill_n(vector) { 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 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)", std_fill_n); #if TEST_STD_VER >= 23 // vector::iterator is not an output_iterator before C++23 bm("rng::fill_n(vector)", std::ranges::fill_n); #endif } benchmark::Initialize(&argc, argv); benchmark::RunSpecifiedBenchmarks(); benchmark::Shutdown(); return 0; }