我正在将一个带有大量系数数组运算的 Matlab 算法移植到 C++,它看起来像这个例子,但通常要复杂得多:
Eigen::Array<double, Dynamic, 1> tx2(12);
tx2 << 1,2,3,4,5,6;
Eigen::Array<double, Dynamic, 1> tx1(12);
tx1 << 7,8,9,10,11,12;
Eigen::Array<double, Dynamic, 1> x = (tx1 + tx2) / 2;
Run Code Online (Sandbox Code Playgroud)
结果证明 C++ 代码比 Matlab 慢得多(大约 20%)。所以在下一步中,我尝试打开 Eigen 的英特尔 MKL 实现,它对性能没有任何作用,就像字面上没有改进一样。MKL 是否有可能不改进系数向量操作?有没有办法测试我是否成功链接了 MKL?有没有比 Eigen::vector 类更快的替代品?提前致谢!
编辑:我在运行 win7 64 位的 i7-3820 上使用 VS 2013。更长的例子是:
Array<double, Dynamic, 1> ts = (k2 / (6 * b.pow(3)) + k / b - b / 2) - (k2 / (6 * a.pow(3)) + k / a - a …Run Code Online (Sandbox Code Playgroud) 我试图用称为细胞的对象填充二维向量(817乘577).这些单元格具有一组成员值(浮点数,其他向量等).在某个时刻程序停止并抛出错误"vector<T> too long".这是单元格的类定义和完整的循环:
struct cell
{
int x;
int y;
int country;
vector<int> popO;
vector<int> popS;
vector<float> Rainfall;
double Cropland;
vector<movement> outm;
vector<movement> inm;
vector<double> AgeMaleFemale;
vector<double> AgeMaleFemaleMortality;
double Fertility;
};
vector<vector<cell>> cells;
void fillCells(dataserver D)
{
cout<<"start filling"<<endl;
int rows=577;
int columns=817;
cell X;
vector<vector<cell>> output(rows,vector<cell>(columns,X));
cout<<"start loop"<<endl;
for (int i=0;i<rows;i++)
{
cout<<i<<" ";
for (int j=0;j<columns;j++)
{
int p=-9999;
cell tmpC;
tmpC.x=i;
tmpC.y=j;
tmpC.country=D.CO[i][j];
tmpC.popO.resize(3,0);
tmpC.popO[0]=int(D.PO[0][i][j]);
tmpC.popO[1]=int(D.PO[1][i][j]);
tmpC.popO[2]=int(D.PO[2][i][j]);
tmpC.Rainfall.resize(10,0);
for (int k=0;k<10;k++)
{
tmpC.Rainfall[k]=D.R[k][i][j];
}
tmpC.popS.resize(10,0); …Run Code Online (Sandbox Code Playgroud)