Is there an easy way to evaluate the column wise dot product of 2 matrices (lets call them A
and B
, of type Eigen::MatrixXd
) that have
I did experiment based on @ggael's answer.
MatrixXd A = MatrixXd::Random(600000,30);
MatrixXd B = MatrixXd::Random(600000,30);
MatrixXd res;
clock_t start, end;
start = clock();
res.noalias() = (A * B.transpose()).diagonal();
end = clock();
cout << "Dur 1 : " << (end - start) / (double)CLOCKS_PER_SEC << endl;
MatrixXd res2;
start = clock();
res2 = (A.array() * B.array()).rowwise().sum();
end = clock();
cout << "Dur 2 : " << (end - start) / (double)CLOCKS_PER_SEC << endl;
MatrixXd res3;
start = clock();
res3 = (A.cwiseProduct(B)).rowwise().sum();
end = clock();
cout << "Dur 3 : " << (end - start) / (double)CLOCKS_PER_SEC << endl;
And the output is:
Dur 1 : 10.442
Dur 2 : 8.415
Dur 3 : 7.576
Seems that the diagonal() solution is the slowest one. The cwiseProduct one is the fastest. And the memory usage is the same.