What's the most efficient way to set the data from a std::vector
to a std::valarray
? Say we have std::valarray<double> my_valarray;
and std::vector<double> my_vector;
and we want to copy the data across from my_vector
to my_valarray
:
Option 1 (using valarray
constructor and copy assignment):
my_valarray = std::valarray(my_vector.data(), my_vector.size());
Option 2 (resizing and copying):
my_valarray.resize(my_vector.size());
std::copy(my_vector.begin(), my_vector.end(), std::begin(my_valarray));
The question arises because in both cases it looks like the complexity is O(2n) . In the first case the data is copied to temporary valarray
during construction (one allocation + one pass to copy the data) and then on assignment to the final object (one allocation + one pass to copy the data). In the second case there is one allocation + one pass for initialisation of all elements to zero and another pass to copy the data. Do the move semantic of C++11 applies in the first case making it require only one allocation and one pass to copy the data?
是的,在第一种情况下适用移动语义,因为std::valarray(my_vector.data(), my_vector.size())
是一个右值,并且为valarray
类定义了移动分配运算符( http://en.cppreference .com / w / cpp / numeric / valarray / operator%3D )。
The first option is more efficient. The reason is that std::valarray::resize
zero-initializes all data. But i would assume that any compiler worth it's salt would optimize that redundant zero-initialization away.
You can't prevent copying from the vector to the valarray, there is no way to transfer the ownership of the memory block from my_vector to my_valarray.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.