Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to optimize this code: char naive_convolve_descr[] = \"naive_convolve: Naive

ID: 3704085 • Letter: H

Question

How to optimize this code:

char naive_convolve_descr[] = "naive_convolve: Naive baseline implementation";
void naive_convolve(int dim, pixel *src, pixel *dst)
{
    int i, j, ii, jj, curI, curJ;

    pixel_sum ps;
    int block = 4;
    for (i = 0; i < dim; i+= block){
        for (j = 0; j < dim; j+=block){
            ps.red    = 0.0;
            ps.green = 0.0;
            ps.blue   = 0.0;
            ps.weight = 0.0;
            for (ii = i+0 ; ii < block; ii++){
  curI = i+ii;  
                for (jj = j+0; jj < block; jj++){                  
                    curJ = j+jj;
                    if((curJ<0 || curJ>=dim) || (curI <0 || curI >=dim)){
                      continue;
                    }
                    ps.red   += src[RIDX(curI, curJ, dim)].red *   kernel[ii+2][jj+2];
                    ps.green += src[RIDX(curI, curJ, dim)].green * kernel[ii+2][jj+2];
                    ps.blue += src[RIDX(curI, curJ, dim)].blue * kernel[ii+2][jj+2];
                    ps.weight += kernel[ii+2][jj+2];

                }
            }
            dst[RIDX(i,j,dim)].red   = (unsigned short)(ps.red/ps.weight);
            dst[RIDX(i,j,dim)].green = (unsigned short)(ps.green/ps.weight);
            dst[RIDX(i,j,dim)].blue = (unsigned short)(ps.blue/ps.weight);

        }
    }
}

Explanation / Answer

Solution:

After evaluating the code given I can see that there is no need of nesting the two outer loops which are running on the variables i and j with the two inner loops which are running with the variables ii and jj.

The changed part is mentioned in Bold.

So the optimized code will be

code:

char naive_convolve_descr[] = "naive_convolve: Naive baseline implementation";
void naive_convolve(int dim, pixel *src, pixel *dst)
{
    int i, j, ii, jj, curI, curJ;

    pixel_sum ps;
    int block = 4;
    for (i = 0; i < dim; i+= block){
        for (j = 0; j < dim; j+=block){
            ps.red    = 0.0;
            ps.green = 0.0;
            ps.blue   = 0.0;
            ps.weight = 0.0;

}

}
            for (ii = i+0 ; ii < block; ii++){
  curI = i+ii;  
                for (jj = j+0; jj < block; jj++){                  
                    curJ = j+jj;
                    if((curJ<0 || curJ>=dim) || (curI <0 || curI >=dim)){
                      continue;
                    }
                    ps.red   += src[RIDX(curI, curJ, dim)].red *   kernel[ii+2][jj+2];
                    ps.green += src[RIDX(curI, curJ, dim)].green * kernel[ii+2][jj+2];
                    ps.blue += src[RIDX(curI, curJ, dim)].blue * kernel[ii+2][jj+2];
                    ps.weight += kernel[ii+2][jj+2];

                }
            }

for (i = 0; i < dim; i+= block){
        for (j = 0; j < dim; j+=block){


            dst[RIDX(i,j,dim)].red   = (unsigned short)(ps.red/ps.weight);
            dst[RIDX(i,j,dim)].green = (unsigned short)(ps.green/ps.weight);
            dst[RIDX(i,j,dim)].blue = (unsigned short)(ps.blue/ps.weight);

}

}
}

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote