syed wrote:

>

> I want clues to optimize a function that copies elements of a

> 2-dimensional array to another. Is there a way to optimize this piece

> of code so that it runs faster?

>

> #define RIDX(i,j,n) ((i)*(n)+(j))

>

> void myrotate(int dim, pixel *src, pixel *dst)

> {

>

> for (i = 0; i < dim; i++)

> for (j = 0; j < dim; j++)

> dst[RIDX(dim-1-j, i, dim)] = src[RIDX(i, j, dim)];

> }
In the "src[RIDX(i,j,dim)]" expression, "i" and "dim" don't change for the

duration of the inner for-loop. Since your RIDX macro will cause these two

same values to be multiplied each time through the loop, you can optimize

this away by calculating this only once:

for (i = 0; i < dim; i++)

{

int temp = i*dim;

for (j = 0; j < dim; j++)

dst[RIDX(dim-1-j, i, dim)] = src[temp+j];

}

You can do this one better by not calcluating the addition each time,

either:

for (i = 0; i < dim; i++)

{

pixel *src2 = &src[RIDX(i,0,dim)];

for (j = 0; j < dim; j++)

dst[RIDX(dim-1-j, i, dim)] = src2[j];

}

A similar optimization can be done on the dst[] array by realizing that the

subscript will be decrementing by dim each time through the loop. Calculate

the initial pointer as "&dst[RIDX(dim-1,i,dim)]" and then decrement that

pointer by "dim" each time through the loop.

for (i = 0; i < dim; i++)

{

pixel *src2 = &src[RIDX(i,0,dim)];

pixel *dst2 = &dst[RIDX(dim-1,i,dim)];

for (j = 0; j < dim; j++)

{

*dst2 = *src2++;

dst2 -= dim;

}

}

You have now eliminated the need for almost all of the multiplcations,

going from dim^2 to dim*2.

--

+-------------------------+--------------------+-----------------------------+

| Kenneth J. Brody |

www.hvcomputer.com | |

| kenbrody/at\spamcop.net |

www.fptech.com | #include <std_disclaimer.h> |

+-------------------------+--------------------+-----------------------------+

Don't e-mail me at: <(E-Mail Removed)>