Skip to content

Commit af7a433

Browse files
committed
[libc++] Add benchmarks for copy algorithms
This patch adds benchmarks for the copy family of algorithms (copy, copy_n, copy_if, copy_backward).
1 parent 5953e5a commit af7a433

File tree

6 files changed

+358
-144
lines changed

6 files changed

+358
-144
lines changed

libcxx/test/benchmarks/algorithms/copy.bench.cpp

Lines changed: 0 additions & 89 deletions
This file was deleted.

libcxx/test/benchmarks/algorithms/copy_backward.bench.cpp

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 <deque>
13+
#include <iterator>
14+
#include <list>
15+
#include <string>
16+
#include <vector>
17+
18+
#include "benchmark/benchmark.h"
19+
#include "../../GenerateInput.h"
20+
21+
template <class Container, class Operation>
22+
void bm_general(std::string operation_name, Operation copy) {
23+
auto bench = [copy](auto& st) {
24+
auto const size = st.range(0);
25+
using ValueType = typename Container::value_type;
26+
Container c;
27+
std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
28+
29+
std::vector<ValueType> out(size);
30+
31+
for ([[maybe_unused]] auto _ : st) {
32+
auto result = copy(c.begin(), c.end(), out.begin());
33+
benchmark::DoNotOptimize(result);
34+
benchmark::DoNotOptimize(out);
35+
benchmark::DoNotOptimize(c);
36+
benchmark::ClobberMemory();
37+
}
38+
};
39+
benchmark::RegisterBenchmark(operation_name, bench)->Range(8, 1 << 20);
40+
}
41+
42+
template <bool Aligned, class Operation>
43+
static void bm_vector_bool(std::string operation_name, Operation copy) {
44+
auto bench = [copy](auto& st) {
45+
auto n = st.range();
46+
std::vector<bool> in(n, true);
47+
std::vector<bool> out(Aligned ? n : n + 8);
48+
benchmark::DoNotOptimize(&in);
49+
auto first = in.begin();
50+
auto last = in.end();
51+
auto dst = Aligned ? out.begin() : out.begin() + 4;
52+
for ([[maybe_unused]] auto _ : st) {
53+
auto result = copy(first, last, dst);
54+
benchmark::DoNotOptimize(result);
55+
benchmark::DoNotOptimize(out);
56+
benchmark::ClobberMemory();
57+
}
58+
};
59+
benchmark::RegisterBenchmark(operation_name, bench)->Range(64, 1 << 20);
60+
}
61+
62+
int main(int argc, char** argv) {
63+
auto std_copy = [](auto first, auto last, auto out) { return std::copy(first, last, out); };
64+
auto ranges_copy = [](auto first, auto last, auto out) { return std::ranges::copy(first, last, out); };
65+
66+
// std::copy
67+
bm_general<std::vector<int>>("std::copy(vector<int>)", std_copy);
68+
bm_general<std::deque<int>>("std::copy(deque<int>)", std_copy);
69+
bm_general<std::list<int>>("std::copy(list<int>)", std_copy);
70+
bm_vector_bool<true>("std::copy(vector<bool>) (aligned)", std_copy);
71+
bm_vector_bool<false>("std::copy(vector<bool>) (unaligned)", std_copy);
72+
73+
// ranges::copy
74+
bm_general<std::vector<int>>("ranges::copy(vector<int>)", ranges_copy);
75+
bm_general<std::deque<int>>("ranges::copy(deque<int>)", ranges_copy);
76+
bm_general<std::list<int>>("ranges::copy(list<int>)", ranges_copy);
77+
bm_vector_bool<true>("ranges::copy(vector<bool>) (aligned)", ranges_copy);
78+
bm_vector_bool<false>("ranges::copy(vector<bool>) (unaligned)", ranges_copy);
79+
80+
benchmark::Initialize(&argc, argv);
81+
benchmark::RunSpecifiedBenchmarks();
82+
benchmark::Shutdown();
83+
return 0;
84+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 <deque>
13+
#include <iterator>
14+
#include <list>
15+
#include <string>
16+
#include <vector>
17+
18+
#include "benchmark/benchmark.h"
19+
#include "../../GenerateInput.h"
20+
21+
template <class Container, class Operation>
22+
void bm_general(std::string operation_name, Operation copy_backward) {
23+
auto bench = [copy_backward](auto& st) {
24+
auto const size = st.range(0);
25+
using ValueType = typename Container::value_type;
26+
Container c;
27+
std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
28+
29+
std::vector<ValueType> out(size);
30+
31+
for ([[maybe_unused]] auto _ : st) {
32+
auto result = copy_backward(c.begin(), c.end(), out.end());
33+
benchmark::DoNotOptimize(result);
34+
benchmark::DoNotOptimize(out);
35+
benchmark::DoNotOptimize(c);
36+
benchmark::ClobberMemory();
37+
}
38+
};
39+
benchmark::RegisterBenchmark(operation_name, bench)->Range(8, 1 << 20);
40+
}
41+
42+
template <bool Aligned, class Operation>
43+
static void bm_vector_bool(std::string operation_name, Operation copy_backward) {
44+
auto bench = [copy_backward](auto& st) {
45+
auto n = st.range();
46+
std::vector<bool> in(n, true);
47+
std::vector<bool> out(Aligned ? n : n + 8);
48+
benchmark::DoNotOptimize(&in);
49+
auto first = in.begin();
50+
auto last = in.end();
51+
auto dst = Aligned ? out.end() : out.end() - 4;
52+
for ([[maybe_unused]] auto _ : st) {
53+
auto result = copy_backward(first, last, dst);
54+
benchmark::DoNotOptimize(result);
55+
benchmark::DoNotOptimize(out);
56+
benchmark::ClobberMemory();
57+
}
58+
};
59+
benchmark::RegisterBenchmark(operation_name, bench)->Range(64, 1 << 20);
60+
}
61+
62+
int main(int argc, char** argv) {
63+
auto std_copy_backward = [](auto first, auto last, auto out) { return std::copy_backward(first, last, out); };
64+
auto ranges_copy_backward = [](auto first, auto last, auto out) {
65+
return std::ranges::copy_backward(first, last, out);
66+
};
67+
68+
// std::copy
69+
bm_general<std::vector<int>>("std::copy_backward(vector<int>)", std_copy_backward);
70+
bm_general<std::deque<int>>("std::copy_backward(deque<int>)", std_copy_backward);
71+
bm_general<std::list<int>>("std::copy_backward(list<int>)", std_copy_backward);
72+
bm_vector_bool<true>("std::copy_backward(vector<bool>) (aligned)", std_copy_backward);
73+
bm_vector_bool<false>("std::copy_backward(vector<bool>) (unaligned)", std_copy_backward);
74+
75+
// ranges::copy
76+
bm_general<std::vector<int>>("ranges::copy_backward(vector<int>)", ranges_copy_backward);
77+
bm_general<std::deque<int>>("ranges::copy_backward(deque<int>)", ranges_copy_backward);
78+
bm_general<std::list<int>>("ranges::copy_backward(list<int>)", ranges_copy_backward);
79+
bm_vector_bool<true>("ranges::copy_backward(vector<bool>) (aligned)", ranges_copy_backward);
80+
bm_vector_bool<false>("ranges::copy_backward(vector<bool>) (unaligned)", ranges_copy_backward);
81+
82+
benchmark::Initialize(&argc, argv);
83+
benchmark::RunSpecifiedBenchmarks();
84+
benchmark::Shutdown();
85+
return 0;
86+
}

0 commit comments

Comments
 (0)