Question: // this is a C language program // please test your code and make sure it is working. // please make sure this program runs
// this is a C language program
// please test your code and make sure it is working.
// please make sure this program runs in less than 5.0 seconds.
In this assignment you will optimize code and get it to run faster. 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 a linux server 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 the way it is - do not change any element of the array * your inner loop should still compute the sum of all the elements of the array. The printf statement for the printing the sum shouldn't be modified * your code should be no longer than 100 lines * your code should not use the "register" keyword * Don't use any mathematical formulas to do the sum. All of the numbers in the array need to have been accessed and used at some time during the running of the inner loop. 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.
---------------------------------------------------------------------------------------
PREMIUM VERSION
Optimize the code in a04.c file so that it runs in less than 5.0 seconds.
Upload your work as file a04.c.
--------------------------------------------------------------------------------
REQUIRED PLATFORM
I grade your code on a Linux system running gcc version 5.4.0 Your code should be portable enough to compile and run correctly on any Linux machine.
GRADING YOUR CODE
Your code needs to compile without errors or warnings and run correctly (use the -Wall flag while developing your code). Code that does not compile will receive 0 points. Code that crashes will receive 0 points.
Please don't use filenames with spaces or '-' in it. These names don't work with my grading scripts.
---------------------------------------------------------------------------------------
a04.c
#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;
double value=0;
// putting some values in the array
for(i=0; i array[i] = value/1000000.0; // You can add variables between this comment ... // ... and this one. // Please change 'your name' to your actual name. printf("CS - Asgmt - I. Forgot "); 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. printf("sum is %10.2f ",sum); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
