Registered Member
|
Hi,
I found random access to large matrix is slower in Eigen than std vector. Testing code is listed below. It's compiled with -O3 flag. Could I make Eigen run faster? Thank you. Xiaoqing ==================================== #include <iostream> #include <vector> #include <Eigen/Dense> #include <time.h> using namespace std; int main() { Eigen::MatrixXf A = Eigen::MatrixXf::Random(100000,100); vector<vector<float> > B(100000,vector<float>(100)); for(int i = 0; i < 100000; ++i) for (int j = 0; j < 100; ++j) B[i][j] = A(i,j); float r = 0; clock_t t1,t2; t1 = clock(); r = 0.0; for(int i = 0; i < 100000; ++i) { int m = rand()%100000; float f = 0; for (int j = 0; j < 100; j++) f += B[m][j]; r += f; } t2 = clock(); cout<<"vector: "<<(double)(t2-t1)/CLOCKS_PER_SEC<<endl; cout<<r<<endl; t1 = clock(); r = 0.0; for(int i = 0; i < 100000; ++i) { int m = rand()%100000; float f = A.row(m).sum(); r += f; } t2 = clock(); cout<<"eigen: "<<(double)(t2-t1)/CLOCKS_PER_SEC<<endl; cout<<r<<endl; return 0; } |
Moderator
|
Note that MatrixXf is column major whereas your std::vector version is row-major. So to make a fair comparison, use:
typedef Matrix<float,Dynamic,Dynamic,RowMajor> RowMajMatrixXf; |
Registered users: bartoloni, Bing [Bot], Evergrowing, Google [Bot], ourcraft