我用本征计算了两个矩阵的内积,第一个是A=(B_C).eval(),第二个是D=(E_F).eval()。这里,B,C,E,F是相同的大小(1500 * 1500),但数值不同。我发现第一个大约200毫秒,第二个大约6000毫秒,我不知道为什么会这样。
#include <iostream>
#include <time.h>
#include "Eigen/Dense"
int main() {
clock_t start, stop;
Eigen::MatrixXf mat_a(1200, 1500);
Eigen::MatrixXf mat_b(1500, 1500);
Eigen::MatrixXf mat_r(1000, 1300);
int i, j;
float c = 0;
for (i = 0; i < 1200; i++) {
for (j = 0; j < 1500; j++) {
mat_a(i, j) = (float)(c/3 * 1.0e-40);
//if (i % 2 == 0 && j % 2 == 0) mat_a(i, j);
c++;
}
}
//std::cout << mat_a.row(0) << std::endl;
c = 100;
for (i = 0; i < 1500; i++) {
for (j = 0; j < 1500; j++) {
mat_b(i, j) = (float)(c/3 * 0.5e-10);
c++;
}
}
//std::cout << mat_b.row(0) << std::endl;
start = clock();
mat_r = mat_a * mat_b;
stop = clock();
std::cout << stop - start << std::endl;
getchar();
return 0;
}如上面的示例代码所示。我发现这是由矩阵的值引起的,当mat_a值为e-40,mat_b值为e-10时,这个问题就会稳定发生。
有人能解释吗?
https://stackoverflow.com/questions/44795503
复制相似问题