Hello, today I encountered this issue. Basically, I have to do some copy of a 2D array to another, these arrays are stores as 1D array.
So here's the code for my 1st loop:
1 2 3 4 5 6 7
for (int j=0; j<halfWidth; j+=samplingRate)
{
for (int n=0; n<N; n++)
{
tmpSrc[n]= src[n*srcWidthStep+j];
}
}
My 2nd part:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
for (int j=0; j<width; j+=samplingRate)
{
int N= min(width-j,height);
for (int ii= height-1, jj=j, n=0; n<N; ii--, jj++, n++)
{
tmpSrc[n]= src[ii*srcWidthStep+jj];
}
}
for (int i=0; i<height; i+=samplingRate)
{
int N= min(i,width);
for (int ii=i, jj=0, n=0; n<N; ii--, jj++, n++)
{
tmpSrc[n]= src[ii*srcWidthStep+jj];
}
}
Basically, the number of loops in 1st and 2nd parts are same, the total number of copy operation is same.
However, the 1st code runs at 0.4 second whilst the second one takes only 0.05 seconds.
Is is that the sequence I read the array src affects the speed? Can anyone enlighten me how to read array efficiently (like make use of caching or something)? Thank you =)
The reason has to do with your hardware and the fact that arrays are laid out in memory columns first (which means that row 0 col 0 is immediately prior to row 0 col 1). To speed it up, you have to flip the indices on your 2D array.