Skip to content

Commit c25e09e

Browse files
authored
[libc++][test] Speed up input generating functions for benchmark tests (#115544)
The input generating functions for benchmark tests in the GenerateInput.h file can be slightly improved by invoking vector::reserve before calling vector::push_back. This slight performance improvement could potentially speed-up all benchmark tests for containers and algorithms that use these functions as inputs.
1 parent 8f8016f commit c25e09e

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

libcxx/test/benchmarks/GenerateInput.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ inline std::vector<IntT> getDuplicateIntegerInputs(std::size_t N) {
5353
template <class IntT>
5454
inline std::vector<IntT> getSortedIntegerInputs(std::size_t N) {
5555
std::vector<IntT> inputs;
56+
inputs.reserve(N);
5657
for (std::size_t i = 0; i < N; i += 1)
5758
inputs.push_back(i);
5859
return inputs;
@@ -61,6 +62,7 @@ inline std::vector<IntT> getSortedIntegerInputs(std::size_t N) {
6162
template <class IntT>
6263
std::vector<IntT> getSortedLargeIntegerInputs(std::size_t N) {
6364
std::vector<IntT> inputs;
65+
inputs.reserve(N);
6466
for (std::size_t i = 0; i < N; ++i)
6567
inputs.push_back(i + N);
6668
return inputs;
@@ -77,6 +79,7 @@ std::vector<IntT> getSortedTopBitsIntegerInputs(std::size_t N) {
7779
template <class IntT>
7880
inline std::vector<IntT> getReverseSortedIntegerInputs(std::size_t N) {
7981
std::vector<IntT> inputs;
82+
inputs.reserve(N);
8083
std::size_t i = N;
8184
while (i > 0) {
8285
--i;
@@ -99,6 +102,7 @@ std::vector<IntT> getPipeOrganIntegerInputs(std::size_t N) {
99102
template <class IntT>
100103
std::vector<IntT> getRandomIntegerInputs(std::size_t N) {
101104
std::vector<IntT> inputs;
105+
inputs.reserve(N);
102106
for (std::size_t i = 0; i < N; ++i)
103107
inputs.push_back(getRandomInteger<IntT>(0, std::numeric_limits<IntT>::max()));
104108
return inputs;
@@ -111,13 +115,15 @@ inline std::vector<std::string> getDuplicateStringInputs(std::size_t N) {
111115

112116
inline std::vector<std::string> getRandomStringInputs(std::size_t N) {
113117
std::vector<std::string> inputs;
118+
inputs.reserve(N);
114119
for (std::size_t i = 0; i < N; ++i)
115120
inputs.push_back(getRandomString(1024));
116121
return inputs;
117122
}
118123

119124
inline std::vector<std::string> getPrefixedRandomStringInputs(std::size_t N) {
120125
std::vector<std::string> inputs;
126+
inputs.reserve(N);
121127
constexpr int kSuffixLength = 32;
122128
const std::string prefix = getRandomString(1024 - kSuffixLength);
123129
for (std::size_t i = 0; i < N; ++i)

0 commit comments

Comments
 (0)