//===----------------------------------------------------------------------===// // // 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_copy = [](auto first, auto last, auto out) { return std::copy(first, last, out); }; // {std,ranges}::copy(normal container) { auto bm = [](std::string name, auto copy) { benchmark::RegisterBenchmark(name, [copy](auto& st) { std::size_t const n = st.range(0); using ValueType = typename Container::value_type; Container c; std::generate_n(std::back_inserter(c), n, [] { return Generate::random(); }); std::vector out(n); for ([[maybe_unused]] auto _ : st) { benchmark::DoNotOptimize(c); benchmark::DoNotOptimize(out); auto result = copy(c.begin(), c.end(), out.begin()); benchmark::DoNotOptimize(result); } })->Range(8, 1 << 20); }; bm.operator()>("std::copy(vector)", std_copy); bm.operator()>("std::copy(deque)", std_copy); bm.operator()>("std::copy(list)", std_copy); bm.operator()>("rng::copy(vector)", std::ranges::copy); bm.operator()>("rng::copy(deque)", std::ranges::copy); bm.operator()>("rng::copy(list)", std::ranges::copy); } // {std,ranges}::copy(vector) { auto bm = [](std::string name, auto copy) { benchmark::RegisterBenchmark(name, [copy](auto& st) { std::size_t const n = st.range(0); std::vector in(n, true); std::vector out(Aligned ? n : n + 8); auto first = in.begin(); auto last = in.end(); auto dst = Aligned ? out.begin() : out.begin() + 4; for ([[maybe_unused]] auto _ : st) { benchmark::DoNotOptimize(in); benchmark::DoNotOptimize(out); auto result = copy(first, last, dst); benchmark::DoNotOptimize(result); } })->Range(64, 1 << 20); }; bm.operator()("std::copy(vector) (aligned)", std_copy); bm.operator()("std::copy(vector) (unaligned)", std_copy); #if TEST_STD_VER >= 23 // vector::iterator is not an output_iterator before C++23 bm.operator()("rng::copy(vector) (aligned)", std::ranges::copy); bm.operator()("rng::copy(vector) (unaligned)", std::ranges::copy); #endif } benchmark::Initialize(&argc, argv); benchmark::RunSpecifiedBenchmarks(); benchmark::Shutdown(); return 0; }