I want to calculate access time for these two ways : Row major and Column major as we know C/C++ is Row major , so when we process in first way (Row major) we should be faster. but look at this code in C++ language
#include <iostream>
#include <time.h>
#include <cstdio>
clock_t RowMajor()
{
char* buf =new char [20000,20000];
clock_t start = clock();
for (int i = 0; i < 20000; i++)
for (int j = 0; j <20000; j++)
{
++buf[i,j];
}
clock_t elapsed = clock() - start;
delete [] buf;
return elapsed ;
}
clock_t ColumnMajor()
{
char* buf =new char[20000,20000];
clock_t start = clock();
for (int i = 0; i < 20000; i++)
for (int j = 0; j < 20000; j++)
{
++buf[j,i];
}
clock_t elapsed = clock() - start;
delete [] buf;
return elapsed ;
}
int main()
{
std::cout << "Process Started." << std::endl;
printf( "ColumnMajor took %lu microSeconds. \n", ColumnMajor()*1000000/ (CLOCKS_PER_SEC) );
printf( "RowMajor took %lu microSeconds. \n", RowMajor() *1000000/ (CLOCKS_PER_SEC) );
std::cout << "done" << std::endl; return 0;
}
but whenever i run this code i get diffrent answers , sometimes Rowmajor time is grater than column major time and sometimes is opposite, any help is apriciated.