c - for-loop optimization using pointer -
i trying optimize code run in under 7 seconds. had down 8, , trying use pointers speed code. gcc gives error when try compile:
.c:29: warning: assignment incompatible pointer type .c:29: warning: comparison of distinct pointer types lacks cast
here had before trying use pointers:
#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; double sum1 = 0; (i = 0; < n_times; i++) { int j; (j = 0; j < array_size; 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; return 0; }
here have when use pointers (this code generates error):
int *j; (j = array; j < &array[array_size]; j += 20) { sum += *j + *(j+1) + *(j+2) + *(j+3) + *(j+4) + *(j+5) + *(j+6) + *(j+7) + *(j+8) + *(j+9); sum1 += *(j+10) + *(j+11) + *(j+12) + *(j+13) + *(j+14) + *(j+15) + *(j+16) + *(j+17) + *(j+18) + *(j+19); }
how fix error? btw don't want suggestions on alternative ways try optimize code. homework problem has constraints i'm allowed do. think once pointer thing fixed run under 7 seconds , i'll go.
comparison of distinct pointer types lacks cast
this means tried compare pointer of 1 type pointer of type, , did without cast.
double *array = calloc(array_size, sizeof(double)); int *j;
pointers double
, pointers int
not directly comparable. aren't allowed compare j
array
reason. perhaps meant declare j
pointer double
?
Comments
Post a Comment