Working with indexes is
defintively easier and more confortable. And probably in many situation the
overhead is not worth to be optimized at all.
I only was surprised when
you said there is a performance gain using indexes.
But I hardly would write
something like this (from your array.cpp example)
for (int i = 0 ; i
< h ; ++i)
for (int j = 0 ;
j < w ; ++j) {
array[i][j] =
uni() ;
}
You are loosing performance
unless the compiler is so intelligent to avoid the multiplication at each step.
When performance is really an issue then I first re-think the algorithm, after
I check the produced assembler listing to see what the compiler did. In the
example above at each step you have to calculate this address:
array + i*rowsize + j
If you are lucky the
compiler can produce code that does this in one instruction set, if not you
will get an overhead. Again if h and w are low values you will not notice it.
This example is of course
much faster, and yes, it is not elegant nor clear.
int *p=array,*pend=&array[h][w];
while (p < pend)
*p++ = uni()
;
Da:
boost-users-bounces@lists.boost.org
[mailto:boost-users-bounces@lists.boost.org] Per
conto di joel falcou
Inviato: giovedė 7 agosto 2008
20.47
A:
Oggetto: Re: [Boost-users] R:
[multi-array] Performance? Difference from GIL or uBLAS?
As a complement, I took time to prepare a sample programms of how I
handle multi-dimensionnal array in my everyday code. It compares it to a
classic pointer access.
I'll be glad to discuss results of such methods on different platform and
discuss pros and cons of this method.
On most platform, whener the data fits in the cache, both methods ends up in a
few tenth of percent of margin. For larger array, the difference grows but it
can be diminished by using loop tiling or loop blocking. Moreover the natural
[][] syntax eases the way you can write complex element acces patterns.