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

Hello, I need help optimize code and get it to run faster. This is what a teache

ID: 3732435 • Letter: H

Question

Hello,

I need help optimize code and get it to run faster. This is what a teacher in my university class wants me to do.

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).

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.

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.

   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

here is your modified code : ----------->>>>>>>>>

#include<stdio.h>
#include<stdlib.h>
#include<time.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 ...
clock_t start,end;
start = clock();
// ... and this one.
for (i = 0; i < N_TIMES; i++) {
  // You can change anything between this comment ...
  int j;
  for (j = 0; j < ARRAY_SIZE; j++) {
   if(i > 0){
    sum = sum + sum/i;
    break;
   }else{
    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 ...
end = clock();
printf("time = %f",(double)(end-start));
// ... and this one.
return 0;
}

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