Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有内存管理的高效编码 我最近从MATLAB切换到C++,以便更快地运行仿真,但是它仍然运行缓慢。我很肯定在内存使用方面还有很多需要改进的地方_C++_Memory Management - Fatal编程技术网

具有内存管理的高效编码 我最近从MATLAB切换到C++,以便更快地运行仿真,但是它仍然运行缓慢。我很肯定在内存使用方面还有很多需要改进的地方

具有内存管理的高效编码 我最近从MATLAB切换到C++,以便更快地运行仿真,但是它仍然运行缓慢。我很肯定在内存使用方面还有很多需要改进的地方,c++,memory-management,C++,Memory Management,考虑下面的代码,它显示了我在模拟中使用的两个数组/向量声明的示例 一个具有已知的固定长度(array01),另一个具有未知长度(array02),在运行期间发生变化 这里的问题是,就内存使用和性能而言,声明变量(对于两种数组类型)的最佳/适当/有效方式是什么 # include <iostream> # include <vector> # include <ctime> # include <algorithm> using namespace

考虑下面的代码,它显示了我在模拟中使用的两个数组/向量声明的示例

一个具有已知的固定长度(array01),另一个具有未知长度(array02),在运行期间发生变化

这里的问题是,就内存使用和性能而言,声明变量(对于两种数组类型)的最佳/适当/有效方式是什么

# include <iostream>
# include <vector>
# include <ctime>
# include <algorithm>
using namespace std;

const int n = 1000;
const int m= 100000;
int main()
{
    srand((unsigned)time(NULL));
    vector <double> array02;
    vector <vector<double>> Array01(n,m);
    for (unsigned int i=0; i<n; i++)
    {
        for (unsigned int j=0; j<m;j++)
        {
             array02.clear();
             rr = rand() % 10;
             for (unsigned int l = 0 ; l<rr <l++)
             {
                  array02.pushback(l);
             }
             // perform some calculation with array01 and array02
         }           
     }
}
#包括
#包括
#包括
#包括
使用名称空间std;
常数int n=1000;
常数int m=100000;
int main()
{
srand((无符号)时间(NULL));
矢量阵列02;
矢量阵列01(n,m);
对于(未签名int i=0;i),您应该考虑用<代码>空格调整(无符号宽度、无符号高度)<代码>成员函数定义定义自己的<代码> <代码>类,以及<代码>双GET(未签名i,未签名j)<代码>内联成员函数和/或<代码>双& AT(un签署i,un签署j)< /COD>内联成员函数。(两者都给出Mi,j元素)。矩阵内部数据可以是一维数组或双精度向量。使用向量向量(大小相同)不是表示矩阵的最佳(或最快)方法

class Matrix {
   std::vector<double> data;
   unsigned width, height;
public:
   Matrix() : data(), width(0), height(0) {};
   ~Matrix() = default;
   /// etc..., see rule of five
   void resize(unsigned w, unsigned h) {
      data.resize(w*h);
      width = w; height = h;
   }
   double get(unsigned i, unsigned j) const { 
      assert(i<width && j<height);
      return data[i*width+j];
   }
   double& at(unsigned i, unsigned j)  { 
      assert(i<width && j<height);
      return data[i*width+j];
   }
}; // end class Matrix
类矩阵{
std::矢量数据;
无符号宽度、高度;
公众:
矩阵():数据(),宽度(0),高度(0){};
~Matrix()=默认值;
///等等,见第五条规则
空心调整大小(无符号w、无符号h){
数据。调整大小(宽*高);
宽度=w;高度=h;
}
双get(无符号i,无符号j)常量{
断言(i 您应该考虑用<代码>空格调整大小(无符号宽度,无符号高度)< /COD>成员函数定义一个自己的<代码> > />代码>成员函数,以及一个<代码>双GET(无符号i,un签署j)< /> >内联成员函数和/或<代码>双& AT(un签署i,un签署j)< /COD>内联成员函数(既给出米,j元素)。矩阵内部数据可以是一维数组或双精度向量。使用向量向量(大小相同)不是表示矩阵的最佳(或最快)方法

class Matrix {
   std::vector<double> data;
   unsigned width, height;
public:
   Matrix() : data(), width(0), height(0) {};
   ~Matrix() = default;
   /// etc..., see rule of five
   void resize(unsigned w, unsigned h) {
      data.resize(w*h);
      width = w; height = h;
   }
   double get(unsigned i, unsigned j) const { 
      assert(i<width && j<height);
      return data[i*width+j];
   }
   double& at(unsigned i, unsigned j)  { 
      assert(i<width && j<height);
      return data[i*width+j];
   }
}; // end class Matrix
类矩阵{
std::矢量数据;
无符号宽度、高度;
公众:
矩阵():数据(),宽度(0),高度(0){};
~Matrix()=默认值;
///等等,见第五条规则
空心调整大小(无符号w、无符号h){
数据。调整大小(宽*高);
宽度=w;高度=h;
}
双get(无符号i,无符号j)常量{

断言(首先,不要试图重新发明轮子:)例如,尝试使用一些高度优化的数值库

  • 英特尔MKL(针对英特尔及兼容处理器的最快、最常用的数学库)
  • LAPACK++(高性能线性代数库)
  • Boost(不仅是数字,而且几乎可以解决任何问题)
第二:如果你需要一个矩阵来编写一个非常简单的程序,可以使用
vector[i+width*j]
表示法。它更快,因为你节省了额外的内存分配

您的示例未编译事件。我尝试稍微重写它:

#include <vector>
#include <ctime>

int main()
{
    const int rowCount = 1000;
    const int columnCount = 1000;

    srand(time(nullptr));

    // Declare matrix
    std::vector<double> matrix;

    // Preallocate elemts (faster insertion later)
    matrix.reserve(rowCount * columnCount);

    // Insert elements
    for (size_t i = 0; i < rowCount * columnCount; ++i) {
        matrix.push_back(rand() % 10);
    }

    // perform some calculation with matrix
    // For example this is a matrix element at matrix[1, 3]:
    double element_1_3 = matrix[3 + 1 * rowCount];

    return EXIT_SUCCESS;
}
#包括
#包括
int main()
{
const int rowCount=1000;
const int columnCount=1000;
srand(时间(nullptr));
//声明矩阵
向量矩阵;
//预分配元素(稍后更快地插入)
矩阵.保留(行数*列数);
//插入元素
对于(大小i=0;i

现在速度取决于
rand()
(这很慢)。

首先,不要尝试重新发明轮子:)例如,尝试使用一些高度优化的数值库

  • 英特尔MKL(针对英特尔及兼容处理器的最快、最常用的数学库)
  • LAPACK++(高性能线性代数库)
  • Boost(不仅是数字,而且几乎可以解决任何问题)
第二:如果你需要一个矩阵来编写一个非常简单的程序,可以使用
vector[i+width*j]
表示法。它更快,因为你节省了额外的内存分配

您的示例未编译事件。我尝试稍微重写它:

#include <vector>
#include <ctime>

int main()
{
    const int rowCount = 1000;
    const int columnCount = 1000;

    srand(time(nullptr));

    // Declare matrix
    std::vector<double> matrix;

    // Preallocate elemts (faster insertion later)
    matrix.reserve(rowCount * columnCount);

    // Insert elements
    for (size_t i = 0; i < rowCount * columnCount; ++i) {
        matrix.push_back(rand() % 10);
    }

    // perform some calculation with matrix
    // For example this is a matrix element at matrix[1, 3]:
    double element_1_3 = matrix[3 + 1 * rowCount];

    return EXIT_SUCCESS;
}
#包括
#包括
int main()
{
const int rowCount=1000;
const int columnCount=1000;
srand(时间(nullptr));
//声明矩阵
向量矩阵;
//预分配元素(稍后更快地插入)
矩阵.保留(行数*列数);
//插入元素
对于(大小i=0;i
现在速度取决于兰德()
(它很慢)。

正如人们所说:

  • 对于矩阵,首选一维数组而不是二维数组
  • 不要重新发明轮子,使用现有的库:从代码中,我认为这是最好的套件。它也有非常非常优化的代码,因为它可以在任何时候使用C++模板静态计算。
      正如人们所说:

      • 对于矩阵,首选一维数组而不是二维数组
      • 不要重新发明轮子,使用现有的库:从代码中,我认为这是最好的套件。它也有非常非常优化的代码,因为它可以在任何时候使用C++模板静态计算。
      矩阵在哪里?:其他操作需要定义它。@BasileStrynkevitch,我使用visual studio express 2010和windows 7。我不熟悉您提到的一些术语,您介意写一个您体验的示例吗