1

I need to store data in a 3D-like structure, however I've been relying on Eigen libraries to handle the matrix structures in my code, and Eigen doesn't supply 3D matrices. I've found two possible workarounds:

 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)

and

 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)

My question is: is there any speed/efficiency advantage in using either method, or are they equivalent?

joaocandre
  • 1,621
  • 4
  • 25
  • 42
  • 2
    Questions concerning the speed or efficiency of different proposed implementations must be answered by measuring in the target environment. It is not possible to give a correct answer without measuring. – Björn Pollex Oct 09 '13 at 19:39
  • 1
    Possible duplicate of [Most efficient option for build 3D structures using Eigen matrices](http://stackoverflow.com/questions/17098218/most-efficient-option-for-build-3d-structures-using-eigen-matrices) – chtz Dec 03 '16 at 20:24

1 Answers1

0

Try this code:

#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;   
}