c - Is this a proper for-loop optimization -
for homework assignment need optimize loop run in under 7.5 seconds. think may have done because code runs in 4 seconds. however, worried not doing correctly because instructor told far under 7.5 seconds wrong. worried might not doing things correctly. here original code:
#include <stdio.h> #include <stdlib.h> #define n_times 600000 #define array_size 10000 int main (void) { double *array = calloc(array_size, sizeof(double)); double sum = 0; int i; (i = 0; < n_times; i++) { int j; (j = 0; j < array_size; j++) { sum += array[j]; } } return 0; }
here optimization:
(i = 0; < n_times; i++) { int j; (j = 0; j < array_size/2; j += 20) { sum += array[j] + array[j+1] + array[j+2] + array[j+3] + array[j+4] + array[j+5] + array[j+6] + array[j+7] + array[j+8] + array[j+9]; sum1 += array[j+10] + array[j+11] + array[j+12] + array[j+13] + array[j+14] + array[j+15] + array[j+16] + array[j+17] + array[j+18] + array[j+19]; } } sum += sum1;
are these doing same number of arithmetic operations? did change code somehow or optimizing well?
your optimizations not correct:
for (j = 0; j < array_size/2; j += 20) {
you loop half many times in inner loop should.
Comments
Post a Comment