【发布时间】:2016-08-03 21:35:01
【问题描述】:
谁能解释特征稀疏矩阵的以下行为?我一直在研究aliasing 和lazy evaluation,但我似乎无法改进这个问题。技术规格:我在 Ubuntu 16.10 上使用最新的 Eigen 稳定版本,带有 g++ 编译器,没有优化标志。
假设我用以下方式定义一个简单的身份:
SparseMatrix<double> spIdent(N,N);
spIdent.reserve(N);
spIdent.setIdentity();
然后用它执行这些操作
spIdent-spIdent;
spIdent*spIdent;
spIdent - spIdent*spIdent;
并测量所有三个的计算时间。我得到的是这个
0 Computation time: 2.6e-05
1 Computation time: 2e-06
2 Computation time: 1.10706
这意味着任何一个操作都很快,但组合起来非常慢。 noalias() 方法只为 dense 矩阵定义,而且在我的密集示例中,它并没有太大的区别。有什么启示吗?
MCVE:
#include <iostream>
#include <ctime>
#include "../Eigen/Sparse"
using namespace std;
using namespace Eigen;
int main() {
unsigned int N=2000000;
SparseMatrix<double> spIdent(N,N);
spIdent.reserve(N);
spIdent.setIdentity();
clock_t start=clock();
spIdent*spIdent;
cout << "0 Computation time: " << float(clock() - start)/1e6 << '\n';
start=clock();
spIdent-spIdent;
cout << "1 Computation time: " << float(clock() - start)/1e6 << '\n';
start=clock();
spIdent - (spIdent*spIdent);
cout << "2 Computation time: " << float(clock() - start)/1e6 << '\n';
return 0;
}
【问题讨论】:
-
呃,使用优化标志?
-
问题不只是让它更快......而是理解为什么第三种情况如此缓慢!
-
前两个可能优化为根本没有代码。第三个不是。
-
在没有开启优化的情况下询问速度,是徒劳的。
-
你能创建一个 MCVE 代码来重现你的计时结果吗? stackoverflow.com/help/mcve