高效的矩阵实现

  • 本文关键字:实现 高效 c++
  • 更新时间 :
  • 英文 :


我有以下问题:

  • 我有一个预先计算好的二维值矩阵,我需要经常查找它,并且只计算一次
  • 矩阵的大小最多约为4000x4000
  • 矩阵不会是稀疏的,我通常需要几乎所有的值
  • 矩阵中的值可以是布尔值、整数或双值。至少它们总是小物件

目前我将预计算的值存储在std::vector<<std::vector<T>>中,我注意到在繁重的计算中,查找该数据结构需要相当长的时间。我在谷歌上搜索了一下,到目前为止,建议的实现方式似乎是尝试一种解决方案,在该解决方案中,所有内存都使用1D数组连续存储,其中该数组中的位置是基于I和j计算的。

有人有一个很好的例子来实现这一点吗?或者有更好的建议吗?我找不到一个现代的C++示例,但这对我来说似乎是一个非常常见的问题。我更喜欢使用别人的代码,而不是在这里重新发明轮子。当然,我会衡量这些差异,看看它是否真的提高了性能。

我发现的例子:

  • https://medium.com/@patdhlk/c-2d-array-adifferent-better-solution-6d371363ebf8
  • https://secure.eld.leidenuniv.nl/~moene/Home/tips/matrix2d/

这里有一个非常简单高效的二维矩阵。"main"创建一个10000x10000的双数组"mat",然后用随机数填充。数组"mat"被复制到另一个数组"mat2"中。您可以输入介于0和9999之间的两个整数"n"one_answers"m",以获取mat2(n,m(处的双数据。

请随意使用或测试它。如果您遇到问题或需要更多功能来实现,请告诉我。祝你好运

#ifndef ytlu_simple_matrix_class_
#define ytlu_simple_matrix_class_
#include <iostream>
#include <iomanip>
#include <complex>
template <typename T> class tMatrix
{
public:
T *ptr;
int col, row, size;
inline T* begin() const {return ptr;}
inline T* end() const {return this->ptr + this->size;}
inline T operator()(const int i, const int j) const { return ptr[i*col+j]; 
} // r-value
inline T&operator()(const int i, const int j) { return ptr[i*col+j]; } //l-value
inline tMatrix(): col{0}, row{0}, size{0}, ptr{0} {;}
tMatrix(const int i, const int j): col(j), row(i), size(i*j)
{
ptr = new T [this->size] ;
}
tMatrix(const tMatrix<T>&a) : tMatrix<T>(a.row, a.col)
{
std::copy(a.begin(), a.end(), this->ptr);
}
tMatrix<T>& operator=(tMatrix<T>&&a)
{
this->col = a.col;
this->row = a.row;
delete [] this->ptr;
this->ptr = a.ptr;
a.ptr = nullptr;
return *this;
}
tMatrix<T>& operator=(const tMatrix<T>&a)
{
if (col==a.cpl && row==a.row) std::copy(a.begin(), a.end(), this->ptr);
else { tMatrix<T>&&v(a); *this = std::move(v);}
return *this;
}
~tMatrix() {delete [] this->ptr;}
}; //end of class tMatrix
template <typename X> std::ostream& operator<<(std::ostream&p, const tMatrix<X>&a)
{
p << std::fixed;
for (int i=0; i<a.row; i++) {
for (int j=0; j <a.col; j++) p << std::setw(12) << a(i, j);
p << std::endl;
}
return p;
}
using iMatrix = tMatrix<int>;
using rMatrix = tMatrix<double>;
using cMatrix = tMatrix<std::complex<double> >;
#endif
//
//
#include <ctime>
#include <cstdlib>
#define N1 10000
int main()
{
int n, m;
std:srand(time(NULL));  // randomize
rMatrix mat(N1, N1);    // declare a 10000 x 10000 double matrix
//
// fill the whole matrix with double random number 0.0 - 1.0
//
for (int i = 0; i<mat.row; i++)
{ for (int j=0; j<mat.col; j++) mat(i, j) = (double)std::rand() / (double)RAND_MAX; }
//
// copy mat to mat 2 just for test
//
rMatrix mat2 = mat;
//
// fetch data test input 0 <= n m < 10000 to print mat2(n, m)
//
while(1)
{
std::cout << "Fetch 2d array at (n m) = ";
std::cin >> n >> m;
if ((n < 0) || (m < 0) || (n > mat2.row) || (m > mat2.col) )break;
std::cout << "mat(" << n << ", " << m << ") = " << mat2(n, m) << std::endl << std::endl;
}
return 0;
}

我使用的编译参数和测试运行。填充随机数字需要几秒钟的时间,我在旧电脑中提取数据时一点也不觉得有错

ytlu@ytlu-PC MINGW32 /d/ytlu/working/cpptest
$ g++ -O3 -s mtx_class.cpp -o a.exe
ytlu@ytlu-PC MINGW32 /d/ytlu/working/cpptest
$ ./a.exe
Fetch 2d array at (n m) = 7000 9950
mat(7000, 9950) = 0.638447
Fetch 2d array at (n m) = 2904 5678
mat(2904, 5678) = 0.655934
Fetch 2d array at (n m) = -3 4

最新更新