I am trying to define a 2D array of 1000 by 1000 ints and then test which transversal
method is faster (row major or column major).
I have this written

#include <stdio.h>
#include <time.h>

int main()
{
    clock_t start, end;
    double elapsed;
    start=clock();

    int array[1000][1000];
    int row, col;
    for(row=0; row<1000; row++)
        for(col=0; col<1000; col++)
            array[row][col]= row*col;
            
   end=clock();
   elapsed= (end-start)/(double)CLOCKS_PER_SEC;
   printf("Seconds elapsed = %f\n", elapsed);
   printf("%d\n", array);
   return 0;

The program compiles, but causes an exception while running. I've tried with smaller
int sizes (10, 100, etc) but they all yield times of 0.000000 seconds, like the array is already optimized.

I am just trying to populate each index in the array with some value so I can run this test. So my question is, can anybody suggest a way that I can populate the 2D array, with each index having some value (e.g. any number between 1 and 100) and calculate the time it takes to execute?

Think about this: What is the address of array[0][0] ?

Another way to get enough time to measure is to do the smaller amount of work multiple times:

for(int i = 0; i < 100; ++i) {
  //do the work
}

Yet another thought: The optimizer may be able to optimize away loops that don't do anything, so you need to do something like increment the value in each cell or keep a running sum of all the values seen or something.

(I would not have filled the array with i*j, though I don't have a good reason why I feel that way. I would have used all 0, or all 1)

Edited 6 Years Ago by griswolf: another thought

Thanks for the quick reply and, interestingly, I thought the same thing. i modified the code to

int array[1000][1000];
	
	for(int row=0; row<1000; row++)
		for(int col=0; col<1000; col++)
			if (row==col)
				array[row][col]=1;
			else
				array[row][col]=2;

Still, when I use smaller int values I don't get a run time. I'm not sure what you mean when you ask me to think about the address of array[0][0]. Isn't it the address of the first element in the array? Which would be a different address to any pointer referring to the array. I'm sorry for my ignorance, I'm not a C programmer, but am very interested in adding it to my languages. I just don't understand why the program works for int values smaller than around 800. Where is the code failing and why?

Address of the array: I wanted you to think about whether an array with a million elements might be too big for the location you supply by simply declaring it. I actually still think this might be an issue, but am not certain. Think about using malloc.

Smaller int values. Hmm. What is UINT_MAX for your platform (OS + compiler)? If for some strange reason it is less than 1,000,000 then there's your problem (I would expect it to be 2^32 == 4,294,967,296 which is much larger)

Smaller array sizes: See my first comment.

Thanks griswolf for the insight. I made a 500X500 array and ran it through 500 iterations and got some meaningful results. One last thing, is there something I'm not doint right with the clock module. I have

start=clock();
//work

end=clock()
elapsed= (end-start)/(double)CLOCKS_PER_SEC;
printf("Row Major took = %f seconds\n", elapsed);

start=clock();
//some work

end= clock();
elapsed= (end-start)/(double)CLOCKS_PER_SEC;
printf("Column Major took = %f seconds\n", elapsed);

It prints the first statement but not the second, the program just keeps running. the "some work" in the second block is the exact same procedure as the first block, I just switched the array[j] to array[j]. I ran that snippet of code on its own and it ran fine. Basically I want to measure the time it take each to run and print the results to the console, all in a single program. like
"Row Major took: xxx.yyy seconds
Column Major took: aaa.bbb seconds"
Any ideas?

Edited 6 Years Ago by ceatkin2: n/a

Never mind, you can mark this thread as solved. Here is my code for anybody who is interested.

#include <stdio.h>
#include <time.h>

int main()
{
    clock_t start, end; 
    double elapsed, elapsed2;
    start=clock();
    int value [500] [500], i,j; // Declaring an Array
    for(int x=0; x<=500; x++)
    {
        for (j = 0; j<500; j++)
        {
            for (i = 0; i<500; i++)
           {
                value [j] [i] = j*100+i; // Loading the Array
           }
        }
   }
  
   printf("\n");
   end=clock();
   
   elapsed= (end-start)/(double)CLOCKS_PER_SEC;
   printf("Row Major took = %f seconds\n", elapsed);
   start=clock();
   int value2[500][500], a, b, c;
    for(int a=0; a<=500; a++)
    {
        for (b = 0; b<500; b++)
        {
            for (c = 0; c<500; c++)
           {
                value2 [c] [b] = c*100+b; // Loading the Array
           }
        }
   }
   
  
 
   end=clock();
   elapsed2= (end-start)/(double)CLOCKS_PER_SEC;
   printf("Column Major took = %f seconds\n", elapsed2);
   if(elapsed2>elapsed)
   {
    printf("Row Major is faster");
    }else{
    printf("Column Major is faster");
   }
   
 
   return 0;

    
   }

It isn't pretty but it's functional. I get row major as being faster in all of my tests, was wondering if this is true in general. Anyway, thanks griswolf for the help. My journey through c just started last week, and you have made the path a little brighter.

This question has already been answered. Start a new discussion instead.