c++ - fastest way to iterate matrix of known dimensions -
i wondering fastest way iterate through matrix in c/c++.
the best method i've come far map matrix single dimension.
then use pointer arithmetic, other method might faster?
dimensions known @ run time not compile time, matrix populated.
#include <iostream> #include <time.h> #define xmax 500 #define ymax 400 #define zmax 300 int main() { srand(0); register double sum = 0; register int i; register int j; register int k; double *arr_ptr; arr_ptr = new double[xmax*ymax*zmax]; (i=0; i<xmax*ymax*zmax; ++i) { *(arr_ptr+i) = rand()/double(rand_max); } clock_t start, finish; start = clock(); (i=0; i<xmax; ++i) { (j=0; j<ymax; ++j) { (k=0; k<zmax; ++k) { sum += *(arr_ptr+i*ymax*zmax+j*zmax+k); } } } finish = clock(); std::cout << "sum: " << sum << "\telapsed: " << finish - start << std::endl; std::cin.get(); delete[] arr_ptr; }
in example bounds constants, normal 3 dimensional arrays work this, c or c++.
then, c , c++ different languages on concerned dynamically allocated arrays variable bounds, don't mix them up. c++ use vector classes , such things. made , should efficient.
in c, since c99 there vla, variable length arrays. contrary urban myth may quite efficient, if don't allocate them on stack. use malloc
big chunk of memory in c.
double (*arr_ptr)[xmax][ymax][zmax] = malloc(sizeof(*arr_ptr)); (register size_t i=0; i<xmax; ++i) (register size_t j=0; j<ymax; ++j) (register size_t k=0; k<zmax; ++k) (*arr_ptr)[i][j][k] = rand()/double(rand_max); . free(arr_ptr);
modern processors have quite complex addressing schemes, might not necessary complete index calculation. compiler knows better you.
then, efficient might more important how declare , handle loop variables. use correct types indexing, size_t
correct unsigned type that. int
may overflow when compute 3-d flattened indices , having signed type here makes not sense.
then declare variables local possible, makes things clearer , compiler.
register
contract compiler never take address of such index. doesn't improve things lot. may prevent doing inefficient things when modify code later.
and last not least, if worry efficiency, check compiler produces. e.g gcc
has option -s
produce intermediate assembler. read instead of speculating efficiency.
Comments
Post a Comment