A commenter in a recent post of mine told me I need to utilize c++11 move-semantics better to deal with a bottleneck in my code. Below is a simplified version of what needs to be fixed.
Question: inside the for loop of makeCopy
, oldV[i]
is an lvalue, so how could I do something like mandatoryCopy[i]&& = oldV[i]
? This is the primary bottleneck, right? I'm thinking something like mandatoryCopy[i]&& = std::move(oldV[i])
, but this obviously won't compile.
#include <iostream>
#include <Eigen/Dense>
#include <vector>
void makeCopy(std::vector<Eigen::VectorXd> &oldV){
int n = oldV.size();
std::vector<Eigen::VectorXd> mandatoryCopy;
mandatoryCopy.resize(n);
for(int i = 0; i < n; i++){
mandatoryCopy[i] = oldV[i];
}
// swap the two
oldV = mandatoryCopy;
}
int main(int argc, char **argv)
{
// starting vector
int len(1000);
Eigen::VectorXd placeHolder(50);
std::vector<Eigen::VectorXd> v(len, placeHolder);
// copy it a bunch of times
for(int iter = 0; iter < 1000; ++iter){
std::cout << "iter: " << iter << "\n";
makeCopy(v);
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire