0

Version A:

#include<time.h>
#include<stdio.h>
int main()
{

time_t start = time(0); //denote start time
int i,j; // initialize ints
static double dst[4096][4096]; //initialize arrays
static double src[4096][4096]; //
for(i=0; i<4096; ++i){
    for(j=0; j<4096; ++j){
        dst[i][j] = src[i][j];
}
    }
time_t end = time(0); //denote end time


double time = difftime(end, start); //take difference of start and end time to determine elapsed time
printf("Test One: %fms\n",time);

}

Version B:

#include<time.h>
#include<stdio.h>
int main()
{

time_t start = time(0); //denote start time
int i,j; // initialize ints
static double dst[4096][4096]; //initialize arrays
static double src[4096][4096]; //
for(i=0; i<4096; ++i){
    for(j=0; j<4096; ++j){
        dst[j][i] = src[j][i];
}
    }
time_t end = time(0); //denote end time


double time = difftime(end, start); //take difference of start and end time to determine elapsed time
printf("Test One: %fms\n",time);

}

Using this program, I have determined that if you reverse the positions of i and j in the arrays, it takes 1 second longer to execute.

Why is this happening?

Curseive
  • 5
  • 1
  • 1
  • 5
  • possible duplicate of [Time taken:Row major sum Column major sum](http://stackoverflow.com/questions/19872481/time-takenrow-major-sum-column-major-sum) – Raymond Chen Nov 19 '13 at 06:19
  • The rather formal explanation you provide suggests that this is a homework assignment rather than a practical programming problem. (Don't forget to credit SO when you turn in your assignment.) – Raymond Chen Nov 19 '13 at 06:55
  • It is actually part of my learning program, not a classroom assignment. It is for my personal knowledge and future application. I have no issue crediting SO for the answer, I am just puzzled as to what a clear explanation would be. – Curseive Nov 19 '13 at 06:56

2 Answers2

3

In your code, the loop means that "traverse the address in the same row, one by one, then go to next line". But if you reverse the positions of i and j, this means that "traverse the address in the same column, one by one, the go to next column".

In C, multi-dimensional array are put on linear address space, byte by byte, then line by line, so dst[i][j] = src[i][j] in your case means *(dst + 4096 * i + j) = *(src + 4096 * i + j):

*(dst + 4096 * 0 + 0) = *(src + 4096 * 0 + 0);
*(dst + 4096 * 0 + 1) = *(src + 4096 * 0 + 1);
*(dst + 4096 * 0 + 2) = *(src + 4096 * 0 + 2);
//...

while reversed i and j means:

*(dst + 4096 * 0 + 0) = *(src + 4096 * 0 + 0);
*(dst + 4096 * 1 + 0) = *(src + 4096 * 1 + 0);
*(dst + 4096 * 2 + 0) = *(src + 4096 * 2 + 0);
//...

So the extra 1 second in second case is cause by accessing memory in a non-contigous manner.

You don't need to do time calculation yourself, because you can run your program with "time" command on linux/UNIX:

$ time ./loop

The results on my linux box for the 2 cases:

$ time ./loop_i_j

real    0m0.244s
user    0m0.062s
sys     0m0.180s

$ time ./loop_j_i

real    0m1.072s
user    0m0.995s
sys     0m0.073s
raof01
  • 76
  • 3
0
#include<time.h>
#include<stdio.h>
int main()
{

time_t start = time(0); //denote start time
int i,j; // initialize ints
static double dst[4096][4096]; //initialize arrays
static double src[4096][4096]; //
for(j=0; j<4096; ++j){
    for(i=0; i<4096; ++i){
        dst[j][i] = src[j][i];
}
    }
time_t end = time(0); //denote end time


double time = difftime(end, start); //take difference of start and end time to determine elapsed time
printf("Test One: %fms\n",time);

}

I tested and it is giving me this o/p Test One: 0.000000ms in both cases after reversing and normal. I used gcc compiler.

Maybe the issue is that you have not included stdio.h .I experienced the same behavior once when I did not include stdio.h.

Something related to memory(in stack) allocation during compile time could be possible reason.

Vinay Shukla
  • 1,818
  • 13
  • 41
  • I actually do have stdio.h included in my code. I must have missed it in copying it over. It would appear that it has to do with my cache size as well, which is 3 MB. Any value smaller than 4096 will return a 0.00... result for both equations. – Curseive Nov 19 '13 at 06:45
  • How does this answer the question, please? And btw, this "*Maybe the issue is that you have not included `stdio.h`.*" is nonsense. – alk Nov 19 '13 at 06:46
  • @Curseive: You know it is about cache. What else do you want to know? – Siyuan Ren Nov 19 '13 at 06:49
  • @C.R. I'm trying to determine the behavior once the array is too large for cache. – Curseive Nov 19 '13 at 06:51