Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

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

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_2

Step: 3

blur-text-image_3

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

Database Systems Introduction To Databases And Data Warehouses

Authors: Nenad Jukic, Susan Vrbsky, Svetlozar Nestorov

1st Edition

1943153191, 978-1943153190

More Books

Students also viewed these Databases questions