Skip to content

Commit c50f43f

Browse files
committed
Simplify how we generate alternating sequences
1 parent 91bf6bd commit c50f43f

File tree

3 files changed

+48
-35
lines changed

3 files changed

+48
-35
lines changed

libcxx/test/benchmarks/algorithms/modifying/remove.bench.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,15 @@ int main(int argc, char** argv) {
4848
constexpr std::size_t BatchSize = 10;
4949
using ValueType = typename Container::value_type;
5050
Container c[BatchSize];
51-
ValueType x = Generate<ValueType>::random();
52-
ValueType y = random_different_from({x});
51+
ValueType x = Generate<ValueType>::random();
52+
ValueType y = random_different_from({x});
53+
auto populate = [&](Container& cont) {
54+
auto half = cont.size() / 2;
55+
std::fill_n(std::fill_n(cont.begin(), half, x), half, y);
56+
};
5357
for (std::size_t i = 0; i != BatchSize; ++i) {
54-
c[i] = Container(size);
55-
auto half = size / 2;
56-
std::fill_n(std::fill_n(c[i].begin(), half, x), half, y);
58+
c[i] = Container(size);
59+
populate(c[i]);
5760
}
5861

5962
while (st.KeepRunningBatch(BatchSize)) {
@@ -66,8 +69,7 @@ int main(int argc, char** argv) {
6669

6770
st.PauseTiming();
6871
for (std::size_t i = 0; i != BatchSize; ++i) {
69-
auto half = size / 2;
70-
std::fill_n(std::fill_n(c[i].begin(), half, x), half, y);
72+
populate(c[i]);
7173
}
7274
st.ResumeTiming();
7375
}
@@ -107,16 +109,17 @@ int main(int argc, char** argv) {
107109
constexpr std::size_t BatchSize = 10;
108110
using ValueType = typename Container::value_type;
109111
Container c[BatchSize];
110-
ValueType x = Generate<ValueType>::random();
111-
ValueType y = random_different_from({x});
112-
auto alternate = [&](auto out, auto n) {
113-
for (std::size_t i = 0; i != n; ++i) {
112+
ValueType x = Generate<ValueType>::random();
113+
ValueType y = random_different_from({x});
114+
auto populate = [&](Container& cont) {
115+
auto out = cont.begin();
116+
for (std::size_t i = 0; i != cont.size(); ++i) {
114117
*out++ = (i % 2 == 0 ? x : y);
115118
}
116119
};
117120
for (std::size_t i = 0; i != BatchSize; ++i) {
118121
c[i] = Container(size);
119-
alternate(c[i].begin(), size);
122+
populate(c[i]);
120123
}
121124

122125
while (st.KeepRunningBatch(BatchSize)) {
@@ -129,7 +132,7 @@ int main(int argc, char** argv) {
129132

130133
st.PauseTiming();
131134
for (std::size_t i = 0; i != BatchSize; ++i) {
132-
alternate(c[i].begin(), size);
135+
populate(c[i]);
133136
}
134137
st.ResumeTiming();
135138
}

libcxx/test/benchmarks/algorithms/modifying/unique.bench.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,15 @@ int main(int argc, char** argv) {
5050
constexpr std::size_t BatchSize = 10;
5151
using ValueType = typename Container::value_type;
5252
Container c[BatchSize];
53-
ValueType x = Generate<ValueType>::random();
54-
ValueType y = random_different_from({x});
53+
ValueType x = Generate<ValueType>::random();
54+
ValueType y = random_different_from({x});
55+
auto populate = [&](Container& cont) {
56+
auto half = cont.size() / 2;
57+
std::fill_n(std::fill_n(cont.begin(), half, x), half, y);
58+
};
5559
for (std::size_t i = 0; i != BatchSize; ++i) {
56-
c[i] = Container(size);
57-
auto half = size / 2;
58-
std::fill_n(std::fill_n(c[i].begin(), half, x), half, y);
60+
c[i] = Container(size);
61+
populate(c[i]);
5962
}
6063

6164
while (st.KeepRunningBatch(BatchSize)) {
@@ -67,8 +70,7 @@ int main(int argc, char** argv) {
6770

6871
st.PauseTiming();
6972
for (std::size_t i = 0; i != BatchSize; ++i) {
70-
auto half = size / 2;
71-
std::fill_n(std::fill_n(c[i].begin(), half, x), half, y);
73+
populate(c[i]);
7274
}
7375
st.ResumeTiming();
7476
}
@@ -108,17 +110,21 @@ int main(int argc, char** argv) {
108110
constexpr std::size_t BatchSize = 10;
109111
using ValueType = typename Container::value_type;
110112
Container c[BatchSize];
111-
ValueType x = Generate<ValueType>::random();
112-
ValueType y = random_different_from({x});
113-
auto alternate = [&](auto out, auto n) {
114-
for (std::size_t i = 0; i != n; i += 2) {
115-
*out++ = (i % 4 == 0 ? x : y);
116-
*out++ = (i % 4 == 0 ? x : y);
113+
ValueType x = Generate<ValueType>::random();
114+
ValueType y = random_different_from({x});
115+
auto populate = [&](Container& cont) {
116+
assert(cont.size() % 4 == 0);
117+
auto out = cont.begin();
118+
for (std::size_t i = 0; i != cont.size(); i += 4) {
119+
*out++ = x;
120+
*out++ = x;
121+
*out++ = y;
122+
*out++ = y;
117123
}
118124
};
119125
for (std::size_t i = 0; i != BatchSize; ++i) {
120126
c[i] = Container(size);
121-
alternate(c[i].begin(), size);
127+
populate(c[i]);
122128
}
123129

124130
while (st.KeepRunningBatch(BatchSize)) {
@@ -130,7 +136,7 @@ int main(int argc, char** argv) {
130136

131137
st.PauseTiming();
132138
for (std::size_t i = 0; i != BatchSize; ++i) {
133-
alternate(c[i].begin(), size);
139+
populate(c[i]);
134140
}
135141
st.ResumeTiming();
136142
}

libcxx/test/benchmarks/algorithms/modifying/unique_copy.bench.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,19 @@ int main(int argc, char** argv) {
9191
std::size_t const size = st.range(0);
9292
using ValueType = typename Container::value_type;
9393
Container c(size);
94-
ValueType x = Generate<ValueType>::random();
95-
ValueType y = random_different_from({x});
96-
auto alternate = [&](auto out, auto n) {
97-
for (std::size_t i = 0; i != n; i += 2) {
98-
*out++ = (i % 4 == 0 ? x : y);
99-
*out++ = (i % 4 == 0 ? x : y);
94+
ValueType x = Generate<ValueType>::random();
95+
ValueType y = random_different_from({x});
96+
auto populate = [&](Container& cont) {
97+
assert(cont.size() % 4 == 0);
98+
auto out = cont.begin();
99+
for (std::size_t i = 0; i != cont.size(); i += 4) {
100+
*out++ = x;
101+
*out++ = x;
102+
*out++ = y;
103+
*out++ = y;
100104
}
101105
};
102-
alternate(c.begin(), size);
106+
populate(c);
103107

104108
std::vector<ValueType> out(size);
105109

0 commit comments

Comments
 (0)