c++ - How to speed std::vector access time -
how improve std::vector time? hi making software multivariable fuzzy k means cluster. work on big matrix´s 50.000 observations 10 variables. matrix not need grow o shrink or bounds check. make resize needed size, load items , make lot of access.
first use:
`std::vector< std::vector<double> > matrix(numclusters, std::vector<double>(numobs,0.0));`
to element do: double a=matrix[i][j]; procces time of 20 minutes.
then make:
std::vector<double> u(numclusters *numobs,0.0);
to element do: double a=u[i*numobs+j]; , time better.
now want make question: more faster access:
iterator+int
std::vector<double>::const_iterator uit = u.begin(); double a= *(uit+index)
pointer[int]
std::vector<double>::const_pointer upt = u.data(); double a= upt[index];
or normal index access[int]
double a= u[index];
greetings
one thing try switch rows , columns. if have 10 × 50,000 matrix , lay down 1 row after another, operations on rows more efficient operations on columns because they'll have better locality. might want consider std::valarray
container should optimize math operations on vector data.
as has been said, using indices vs. pointers shouldn't matter far efficiency concerned. indices more readable.
a c++ thing might want (which shouldn't have effects on efficiency, code readability) wrap vector in container makes behave 2d matrix uses contiguous 1d vector underneath. take @ how can use std::valarray store/manipulate contiguous 2d array? inspiration.
Comments
Post a Comment