84 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++1710 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_copy = [](auto first, auto last, auto out) { return std::copy(first, last, out); };25 26 // {std,ranges}::copy(normal container)27 {28 auto bm = []<class Container>(std::string name, auto copy) {29 benchmark::RegisterBenchmark(name, [copy](auto& st) {30 std::size_t const n = st.range(0);31 using ValueType = typename Container::value_type;32 Container c;33 std::generate_n(std::back_inserter(c), n, [] { return Generate<ValueType>::random(); });34 35 std::vector<ValueType> out(n);36 37 for ([[maybe_unused]] auto _ : st) {38 benchmark::DoNotOptimize(c);39 benchmark::DoNotOptimize(out);40 auto result = copy(c.begin(), c.end(), out.begin());41 benchmark::DoNotOptimize(result);42 }43 })->Range(8, 1 << 20);44 };45 bm.operator()<std::vector<int>>("std::copy(vector<int>)", std_copy);46 bm.operator()<std::deque<int>>("std::copy(deque<int>)", std_copy);47 bm.operator()<std::list<int>>("std::copy(list<int>)", std_copy);48 bm.operator()<std::vector<int>>("rng::copy(vector<int>)", std::ranges::copy);49 bm.operator()<std::deque<int>>("rng::copy(deque<int>)", std::ranges::copy);50 bm.operator()<std::list<int>>("rng::copy(list<int>)", std::ranges::copy);51 }52 53 // {std,ranges}::copy(vector<bool>)54 {55 auto bm = []<bool Aligned>(std::string name, auto copy) {56 benchmark::RegisterBenchmark(name, [copy](auto& st) {57 std::size_t const n = st.range(0);58 std::vector<bool> in(n, true);59 std::vector<bool> out(Aligned ? n : n + 8);60 auto first = in.begin();61 auto last = in.end();62 auto dst = Aligned ? out.begin() : out.begin() + 4;63 for ([[maybe_unused]] auto _ : st) {64 benchmark::DoNotOptimize(in);65 benchmark::DoNotOptimize(out);66 auto result = copy(first, last, dst);67 benchmark::DoNotOptimize(result);68 }69 })->Range(64, 1 << 20);70 };71 bm.operator()<true>("std::copy(vector<bool>) (aligned)", std_copy);72 bm.operator()<false>("std::copy(vector<bool>) (unaligned)", std_copy);73#if TEST_STD_VER >= 23 // vector<bool>::iterator is not an output_iterator before C++2374 bm.operator()<true>("rng::copy(vector<bool>) (aligned)", std::ranges::copy);75 bm.operator()<false>("rng::copy(vector<bool>) (unaligned)", std::ranges::copy);76#endif77 }78 79 benchmark::Initialize(&argc, argv);80 benchmark::RunSpecifiedBenchmarks();81 benchmark::Shutdown();82 return 0;83}84