I am new to this, can someone please help! In this assignment you will optimize
ID: 3625837 • Letter: I
Question
I am new to this, can someone please help!In this assignment you will optimize code and get it to run faster using techniques you have learned in this course.
In this program measure code execution time using the Linux time 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.
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. The code that you should start with is attached here.
Do not use the optimization features of gcc. I will be compiling your code with all gcc optimizations turned off. Compile your code like this: gcc -Wall -g a02.c -o a02
Code that runs in less than 5.0 seconds will potentially earn full credit.
// This is your starting code.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Submitted code must have the correct values for these two constants:
// N_TIMES: 200000
// ARRAY_SIZE: 9973
#define N_TIMES 200000
#define ARRAY_SIZE 9973
int main (void)
{
int *array = calloc(ARRAY_SIZE, sizeof(int));
int sum = 0;
int checksum = 0;
int i;
int j;
int x;
// Initialize the array with random values 0 to 13.
//
srand(time(NULL));
for (j=0; j < ARRAY_SIZE; j++) {
x = rand() / (int)(((unsigned)RAND_MAX + 1) / 14);
array[j] = x;
checksum += x;
}
//printf("Checksum is %d. ",checksum);
for (i = 0; i < N_TIMES; i++) {
// ---------------------------------------------------------------
// Do not alter anything above this line, except for any variables that need to be declared
// outside of the i loop. Here is where you make your changes to
// optimize performance.
for (j=0; j < ARRAY_SIZE; j++) {
sum += array[j];
// printf("Sum is now: %d ",sum);
}
// Do not alter anything below this line.
// ---------------------------------------------------------------
// Check each iteration.
//
if (sum != checksum) {
printf("Checksum error! ");
}
sum = 0;
}
return 0;
Explanation / Answer
for (i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.