Skip to content
Advertisement

Optimizing C for loops [closed]

I have been trying to optimize my program so far I got down to 8 seconds but my goal its to make it less than 5 seconds. What else could I change in my 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, j;

    printf("CS201 - Asgmt 4 - your namen");

    for (i = 0; i < N_TIMES; i++) {
        for (j = 0; j < ARRAY_SIZE; j = j + 20) {
            sum = 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] + 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];
        }   
    }
}

Advertisement

Answer

Remove all of the meaningless process.

#include <stdio.h>

int main(void) {
    printf("CS201 - Asgmt 4 - your namen");
    return 0;
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement