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

In this assignment you will optimize code and get it to run faster using techniq

ID: 3730232 • Letter: I

Question

In this assignment you will optimize code and get it to run faster using
techniques you have learned in this course.

You will measure code execution time using the Linux time(1) command. This
command prints out three numbers:

    real: total elapsed time
    user: total cpu usage of the application code
    sys: total cpu usage of the application's operating system calls

For the purposes of this assignment, you will use the user statistic returned by
time(1).

You should do this work on syccuxas01.pcc.edu. This is where I will be testing
your code, and execution times on this machine are the standard that your code
will be judged by. The fact that your code may have met the timing standard on
some other machine does not count, and will not help you.

The code for this assignment has an outer loop and an inner loop. The inner loop
computes the sum of the numbers in an array (note that the array is initialized
by calloc() to all zeroes). The outer loop repeats the inner loop's computation
the specified number of times. You should maintain this loop structure as you
optimize this code. In particular:

    * you should not alter the outer loop
    * you should not change the size of the array or its data type
    * the array should remain initialized to all zeroes -
        do not change any element of the array
    * your inner loop should still compute the sum of all the elements
        of the array
    * your code should be no longer than 100 lines
    * your code should not use the "register" keyword

The basic idea is to change the contents of the inner loop to make the
computation run faster while having it still repeatedly compute the sum of the
array elements.

Do not write any assembly language for this assignment. You can achieve the
speedup you need at the C code level.

Do not use the 'register' keyword. There's a good chance it will produce strange,
irrelevant, and completely wrong results for you. You can accomplish everything
you need to without using it.

Optimize the code in so that it runs in less than 5.0 seconds.

#include <stdio.h>
#include <stdlib.h>

// You are only allowed to make changes to this code as specified by the comments in it.

// The code you submit must have these two values.
#define N_TIMES       600000
#define ARRAY_SIZE   10000

int main(void)
{
   double   *array = calloc(ARRAY_SIZE, sizeof(double));
   double   sum = 0;
   int       i;

   // You can add variables between this comment ...

   // ... and this one.

   // Please change 'your name' to your actual name.

   for (i = 0; i < N_TIMES; i++) {

       // You can change anything between this comment ...

       int     j;

       for (j = 0; j < ARRAY_SIZE; j++) {
           sum += array[j];
           }

       // ... and this one. But your inner loop must do the same
       // number of additions as this one does.

       }

   // You can add some final code between this comment ...

   // ... and this one.

   return 0;
}

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

#define N_TIMES       600000
#define ARRAY_SIZE   10000
int main(void)
{
    double *array = calloc(ARRAY_SIZE, sizeof(double));
    double sum = 0;                                               

    int     i;

    double* j;
    double sum0 = 0;
    double sum1 = 0;
    double sum2 = 0;
    double sum3 = 0;
    double sum4 = 0;
    printf("CS201 - Asgmt 4 - xxxxxxxxxx ");

    for (i = 0; i < N_TIMES; i++) {
      
        j = array + (ARRAY_SIZE);   
        while(array < j - 15) {    
            sum0 += *j + *(j - 1) + *(j -2)+ *(j -3);
            sum1 += *(j - 4) + *(j -5)+ *(j -6);
            sum2 += *(j - 7) + *(j -8)+ *(j -9);
            sum3 += *(j - 10) + *(j -11)+ *(j - 12);
            sum4 += *(j - 13) + *(j -14);
            j-=15;
            }
     
        if (j > array) {
            sum0 += *j;
            }
    }

    sum += (sum0 + sum1 + sum2 + sum3 + sum4);
   
    return 0;
}