I wrote some C++ code for matrix multiplication. I used vector<double>
to save the matrix entries, and I used a series of 3 nested for
loops to calculate the multiplication entry-by-entry. It turns out that this is super slow (for a 900*500 and 500*500 matrix multiplication, it takes about 10 seconds on my macbook air). What is the reason? Did I use a bad representation of matrix or there are big flaws in the code?
for (int c_b=0;c_b<B.n_c;c_b++)
{
vector<double> vtmp(A.n_r);
for (int r_a=0;r_a<A.n_r;r_a++)
{
sum=0;
for (int i=0;i < A.n_c;i++)
{
sum=sum+A.mat[r_a+i*A.n_r]*B.mat[i+c_b*B.n_r];
}
vtmp[r_a]=sum;
}
Cvv[c_b]=vtmp;
}
UPDATE: This issue has been resolved by using subroutines in Lapack.