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// UNSUPPORTED: c++03, c++11, c++14, c++17
10
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
22int main(int argc, char** argv) {
23 auto std_transform = [](auto first, auto last, auto out, auto f) { return std::transform(first, last, out, f); };
24
25 // {std,ranges}::transform(normal container)
26 {
27 auto bm = []<class Container>(std::string name, auto transform) {
28 benchmark::RegisterBenchmark(
29 name,
30 [transform](auto& st) {
31 std::size_t const size = st.range(0);
32 using ValueType = typename Container::value_type;
33 Container c;
34 std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
35
36 std::vector<ValueType> out(size);
37
38 auto f = [](auto element) {
39 benchmark::DoNotOptimize(element);
40 return element;
41 };
42
43 for ([[maybe_unused]] auto _ : st) {
44 benchmark::DoNotOptimize(c);
45 benchmark::DoNotOptimize(out);
46 auto result = transform(c.begin(), c.end(), out.begin(), f);
47 benchmark::DoNotOptimize(result);
48 }
49 })
50 ->Arg(32)
51 ->Arg(50) // non power-of-two
52 ->Arg(1024)
53 ->Arg(8192);
54 };
55 bm.operator()<std::vector<int>>(name: "std::transform(vector<int>) (identity transform)", transform: std_transform);
56 bm.operator()<std::deque<int>>(name: "std::transform(deque<int>) (identity transform)", transform: std_transform);
57 bm.operator()<std::list<int>>(name: "std::transform(list<int>) (identity transform)", transform: std_transform);
58 bm.operator()<std::vector<int>>("rng::transform(vector<int>) (identity transform)", std::ranges::transform);
59 bm.operator()<std::deque<int>>("rng::transform(deque<int>) (identity transform)", std::ranges::transform);
60 bm.operator()<std::list<int>>("rng::transform(list<int>) (identity transform)", std::ranges::transform);
61 }
62
63 benchmark::Initialize(&argc, argv);
64 benchmark::RunSpecifiedBenchmarks();
65 benchmark::Shutdown();
66 return 0;
67}
68

source code of libcxx/test/benchmarks/algorithms/modifying/transform.unary.bench.cpp