I want to asynchronously process a std::vector<int>
within my program. The retrieval of the objects, however, do not utilize move semantics.
I've created a minimal working example and attached it below. According to cplusplus.com, move semantics on copying and constructing vectors are linear in the size of the moved-to object and constant in the size of the moved-from object, as long as they share the same allocator (I'm using the standard one).
According to cplusplus.com again, when retrieving an object from std::future<T>::get()
and T
is neither void
nor a reference type (which is not the case), it behaves like moving the value.
I even tried to just use inputs[i].get();
instead in the second loop, currently within the comments, and not assigning it to anything. This still give the linear time increase.
std::vector<std::vector<int>> GenerateTestCases(int input_size, int number_inputs) {
std::vector<std::vector<int>> cases(number_inputs);
for (auto i = 0; i < number_inputs; i++) {
std::vector<int> some_vector(input_size);
cases[i] = std::move(some_vector);
}
return std::move(cases);
}
int main() {
for (auto i = 0; i < 25; i++) {
auto size = (int)pow(2, i);
int iterations = 100;
auto test_cases = GenerateTestCases(size, iterations);
std::vector<std::future<std::vector<int>>> inputs(iterations);
const auto start = std::chrono::high_resolution_clock::now();
for (auto i = 0; i < test_cases.size(); i++) {
std::promise<std::vector<int>> prom;
prom.set_value(std::move(test_cases[i]));
inputs[i] = std::move(prom.get_future());
}
const auto middle = std::chrono::high_resolution_clock::now();
for (auto i = 0; i < test_cases.size(); i++) {
//inputs[i].get();
auto& result = (inputs[i]);
auto value = std::move(result.get());
}
const auto end = std::chrono::high_resolution_clock::now();
const auto elapsed_first = std::chrono::duration_cast<std::chrono::nanoseconds>
(middle - start).count();
const auto elapsed_second = std::chrono::duration_cast<std::chrono::nanoseconds>
(end - middle).count();
std::cout << "First: " << elapsed_first << std::endl;
std::cout << "Second: " << elapsed_second << std::endl;
std::cout << std::endl;
}
char c;
std::cin >> c;
}
I, however, see a linear increase in execution time when I'm executing my code above, ranging from
First: 13440ns Second: 9919ns
for the smallest arrays size to
First: 25919ns Second: 300147450ns
for the largest one.
I'm using VS2019 on Win10 and compiling for x64, if this is from interest.