c++ - Efficient coding with memory management -


i've switched matlab c++ in order run simulations faster, still runs slow. i'm pretty positive there improve in terms of memory usage.

consider following code, shows example of 2 array/vector declaration, use in simulation.

one known fixed length (array01) , unknown length (array02) changes during run.

the question here best/proper/efficient way of declaring variables ( both array types) in terms of memory usage , performance.

# 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);     (unsigned int i=0; i<n; i++)     {         (unsigned int j=0; j<m;j++)         {              array02.clear();              rr = rand() % 10;              (unsigned int l = 0 ; l<rr <l++)              {                   array02.pushback(l);              }              // perform calculation array01 , array02          }                 } } 

you should consider defining own matrix class void resize(unsigned width, unsigned height) member function, , double get(unsigned i, unsigned j) inlined member function and/or double& at(unsigned i, unsigned j) inlined member function (both giving mi,j element). matrix internal data one-dimensional array or vector of doubles. using vector of vectors (all of same size) not best (or fastest) way represent matrix.

class matrix {    std::vector<double> data;    unsigned width, height; public:    matrix() : data(), width(0), height(0) {};    ~matrix() = default;    /// etc..., see rule of 5    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 

read rule of five.

you try scilab (it free software). similar matlab , might have different performances. don't forget use recent version.

btw, there tons of existing c++ numerical libraries dealing matrices. consider using 1 of them. if performance of paramount importance, don't forget ask compiler optimize code after have debugged it.

assuming on linux (which recommend numerical computations; significant supercomputers run linux), compile using g++ -std=c++11 -wall -wextra -g during debugging phase, use g++ -std=c++11 -wall -wextra -mtune=native -o3 during benchmarking. don't forget profile, , remember premature optimization evil (you first need make program correct).

you might spend weeks, or months , perhaps many years, of work use techniques openmp, opencl, mpi, pthreads or std::thread parallelization (which difficult subject you'll need years master).

if matrix big, and/or have additional properties (is sparse, triangular, symmetric, etc...) there many mathematical , computer science knowledge master improve performance. can make phd on that, , spend entire life on subject. go university library read books on numerical analysis , linear algebra.

for random numbers c++11 gives <random>; btw use c++11 or c++14, not earlier version of c++.

read http://floating-point-gui.de/ , book c++ programming.

ps. don't claim particular expertise on numerical computation. prefer symbolic computation.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -