我是C ++和Eigen库的新手。我想在大小为1815 X 1815
的矩阵上执行LU分解(部分透视),并带有复杂的条目。但是,我的代码性能很差,LU分解花费77.2852
秒,而MATLAB仅花费0.140946
秒。请找到随附的代码。关于如何改进代码的任何建议?请注意,在代码的第一部分中,我正在从条目为a + bi
的文件中导入矩阵,其中a
和b
是复数。矩阵文件是从MATLAB生成的。谢谢。
#include <iostream>
#include <Eigen/Dense>
#include <fstream>
#include <complex>
#include <string>
#include <chrono>
using namespace std;
using namespace std::chrono;
using namespace Eigen;
int main(){
int mat_sz = 1815; // size of matrix
MatrixXcd c_mat(mat_sz,mat_sz); // initialize eigen matrix
double re, im;
char sign;
string entry;
ifstream myFile("A_mat"); // format of entries : a + bi. 'a' and 'b' are complex numbers
//Import and assign matrix to an Eigen matrix
for (int i = 0; i < mat_sz; i++){
for (int j = 0; j < mat_sz; j++){
myFile >> entry;
stringstream stream(entry);
stream >> re >> sign >> im;
c_mat(i,j) = {re, (sign == '-') ? -im : im}; // Assigning matrix entries
}
}
// LU Decomposition
auto start = high_resolution_clock::now();
c_mat.partialPivLu(); // Solving equation through partial LU decomposition
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
double million = 1000000;
cout << "Time taken by function: " << duration.count()/million << " seconds" << endl;
}
我将评论总结为一个答案。
[当您感觉到Eigen
的运行速度很慢时,有一些事情需要验证。
-O1
或更高,MSVC中的/O1
或更高)。有关优化的一般说明可以为found here。