c++ - Difference in speed computation between STL vector of vectors and contiguous storage -
in program have scroll lot of time 2d array , computation on it; in first way saved matrix vector<vector<int>>;. after tried contiguous memory 2d array
template <typename t> t** create2darray(unsigned nrows, unsigned ncols) { t** ptr = new t*[nrows]; // allocate pointers t* pool = new t[nrows*ncols]; // allocate pool (unsigned = 0; < nrows; ++i, pool += ncols ) ptr[i] = pool; return ptr; } template <typename t> void delete2darray(t** arr) { delete [] arr[0]; // remove pool delete [] arr; // remove pointers } int main() { double **dptr = create2darray<double>(10,10); dptr[0][0] = 10; // example delete2darray(dptr); // free memory } and saw vector of vectors storaging computation speed more or less 2 time faster contiguous way.i don't understand why.
edit :i have matrix of integer made 0,1,2 , have read file in stored matrix cars (1=bluecar, 2=redcar, 0=empty).
i need write algorithm move cars of matrix in way:
- blue ones move downward;
- red ones move rightward;
- there turn in blue ones move , turn move red ones.
i have 2 vectors have coordinates of non 0 cell of matrix,and scroll matrix through these vectors. have access lot of time matrix , change it.
Comments
Post a Comment