Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 #include

// 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; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions