83 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_n = [](auto first, auto n, auto out) { return std::copy_n(first, n, out); };25 26 // {std,ranges}::copy_n(normal container)27 {28 auto bm = []<class Container>(std::string name, auto copy_n) {29 benchmark::RegisterBenchmark(name, [copy_n](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_n(c.begin(), n, out.begin());41 benchmark::DoNotOptimize(result);42 }43 })->Range(8, 1 << 20);44 };45 bm.operator()<std::vector<int>>("std::copy_n(vector<int>)", std_copy_n);46 bm.operator()<std::deque<int>>("std::copy_n(deque<int>)", std_copy_n);47 bm.operator()<std::list<int>>("std::copy_n(list<int>)", std_copy_n);48 bm.operator()<std::vector<int>>("rng::copy_n(vector<int>)", std::ranges::copy_n);49 bm.operator()<std::deque<int>>("rng::copy_n(deque<int>)", std::ranges::copy_n);50 bm.operator()<std::list<int>>("rng::copy_n(list<int>)", std::ranges::copy_n);51 }52 53 // {std,ranges}::copy_n(vector<bool>)54 {55 auto bm = []<bool Aligned>(std::string name, auto copy_n) {56 benchmark::RegisterBenchmark(name, [copy_n](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 dst = Aligned ? out.begin() : out.begin() + 4;62 for ([[maybe_unused]] auto _ : st) {63 benchmark::DoNotOptimize(in);64 benchmark::DoNotOptimize(out);65 auto result = copy_n(first, n, dst);66 benchmark::DoNotOptimize(result);67 }68 })->Range(64, 1 << 20);69 };70 bm.operator()<true>("std::copy_n(vector<bool>) (aligned)", std_copy_n);71 bm.operator()<false>("std::copy_n(vector<bool>) (unaligned)", std_copy_n);72#if TEST_STD_VER >= 23 // vector<bool>::iterator is not an output_iterator before C++2373 bm.operator()<true>("rng::copy_n(vector<bool>) (aligned)", std::ranges::copy_n);74 bm.operator()<false>("rng::copy_n(vector<bool>) (unaligned)", std::ranges::copy_n);75#endif76 }77 78 benchmark::Initialize(&argc, argv);79 benchmark::RunSpecifiedBenchmarks();80 benchmark::Shutdown();81 return 0;82}83