versus the same, but with the loops nested the other wayCode:#include <stdio.h> #define IMGX 8192 #define IMGY 8192 int red_freq[256]; char img[IMGY][IMGX][3]; main () { int i, j; long long total; long long redness; for (i = 0; i < 256; i++) red_freq[i] = 0; for (i = 0; i < IMGY; i++) for (j = 0; j < IMGX; j++) red_freq[img[i][j][0]] += 1; total = 0; for (i = 0; i < 256; i++) total += (long long)i * (long long)red_freq[i]; redness = (total + (IMGX*IMGY/2))/(IMGX*IMGY); printf("The average redness of the image is %lld.\n", redness); }
When executing, the first one is faster than the second, but I don't understand why. Any thoughts?Code:for (j = 0; j < IMGX; j++) for (i = 0; i < IMGY; i++) red_freq[img[i][j][0]] += 1;