Boost logo

Ublas :

From: Stephen Gross (sgross_at_[hidden])
Date: 2007-03-23 13:02:48

I've been testing out the performance of assigning individual elements in a
matrix. I have a pretty simple code snippet that creates an NxN matrix, loops
across the elements, and assigns them repeatedly. I tried it out first using the
matrix's operator(), and then using a pointer to the underlying array. I found
that accessing the array directly was a *lot* faster! Is this correct? Should I
expect to see better performance here? I would have thought that simple element
access would be pretty fast already...?

Any thoughts?


Code snippet follows:
const int size = 500;
ublas::matrix<double, ublas::column_major> m(size, size);

// First example uses operator():
for(int i = 0; i < size; ++i)
  for(int j = 0; j < size; ++j)
     for(int z = 0; z < size; ++z)
        m(i, j) = z;

// Second, faster example uses direct access:
double * array_begin = &*;

for(int i = 0; i < size; ++i)
  for(int j = 0; j < size; ++j)
     for(int z = 0; z < size; ++z)
        *(array_begin + (j * size) + i) = z;