2013-10-09 48 views
1

我需要存儲數據在類似3D的結構,但我一直依靠特徵庫來處理我的代碼中的矩陣結構,而Eigen不提供3D矩陣。我已經發現了兩個可能的解決方法:最有效的方法來舉行若干Eigen MatrixXd

int x,y,z; 
Eigen::Matrix<Eigen::Matrix<double,Dynamic,Dynamic>, Dynamic,1> M(z); 
for (int i = 0; i < M.rows(); ++i) M(i)=MatrixXd::Zero(x,y); 

// access coefficients with M(z)(x,y) 

int x,y,z; 
std::vector<Eigen::Matrix<double,Dynamic,Dynamic> > M(z); 
for (int i = 0; i < M.rows(); ++i) M[i]=MatrixXd::Zero(x,y); 

// access coefficients with M[z](x,y) 

我的問題是:是否有任何速度/效率優勢,在使用這兩種方法,或者是他們相同呢?

+2

問題必須在目標環境中測量來回答。沒有測量就無法給出正確的答案。 –

+1

[使用特徵矩陣構建3D結構的最高效選項]的可能副本(http://stackoverflow.com/questions/17098218/most-efficient-option-for-build-3d-structures-using-eigen-matrices) – chtz

回答

0

試試這個代碼:

關於速度或不同的提議實現的效率
#include<windows.h> 

LARGE_INTEGER startTime, stopTime; 
LARGE_INTEGER freq; 

int main(int argc, char *argv[]) 
{ 
    QueryPerformanceFrequency(&freq); 

    // ACCESS TIME 1 
    QueryPerformanceCounter(&startTime); 

    int x1,y1,z1; 
    Eigen::Matrix<Eigen::Matrix<double,Dynamic,Dynamic>, Dynamic,1> M1(z1); 
    for (int i = 0; i < M1.rows(); ++i) M1(i)=MatrixXd::Zero(x1,y1); 

    QueryPerformanceCounter(&stopTime); 
    double msecs1= (double)(stopTime.QuadPart - startTime.QuadPart)/(double)freq.QuadPart; 

    // ACCESS TIME 2 
    QueryPerformanceCounter(&startTime); 

    int x2,y2,z2; 
    std::vector<Eigen::Matrix<double,Dynamic,Dynamic> > M2(z2); 
    for (int i = 0; i < M2.rows(); ++i) M2[i]=MatrixXd::Zero(x2,y2); 

    QueryPerformanceCounter(&stopTime); 
    double msecs2= (double)(stopTime.QuadPart - startTime.QuadPart)/(double)freq.QuadPart; 

    // RESULT 
    cout<<"t1="<<msecs1<<", t2="<<msecs2; 
} 
+0

我正在運行linux。任何替代'windows.h'? – joaocandre

+0

http://stackoverflow.com/a/588377/1106369 –