gpt4 book ai didi

c++ - 持有多个 Eigen MatrixXd 的最有效方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:35:49 27 4
gpt4 key购买 nike

我需要以类似 3D 的结构存储数据,但是我一直依赖 Eigen 库来处理代码中的矩阵结构,而 Eigen 不提供 3D 矩阵。我发现了两种可能的解决方法:

 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)

我的问题是:使用这两种方法是否有任何速度/效率优势,或者它们是否等效?

最佳答案

试试这段代码:

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

关于c++ - 持有多个 Eigen MatrixXd 的最有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19280931/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com