Row | C program showing time difference in row major and column major access ; taking MAX 10000 so that time difference can be shown ; accessing element row wise ; accessing element column wise ; driver code ; Time taken by row major order ; Time taken by column major order
#include <stdio.h> NEW_LINE #include <time.h> NEW_LINE #define MAX  10000 NEW_LINE int arr [ MAX ] [ MAX ] = { 0 } ; void rowMajor ( ) { int i , j ; for ( i = 0 ; i < MAX ; i ++ ) { for ( j = 0 ; j < MAX ; j ++ ) { arr [ i ] [ j ] ++ ; } } } void colMajor ( ) { int i , j ; for ( i = 0 ; i < MAX ; i ++ ) { for ( j = 0 ; j < MAX ; j ++ ) { arr [ j ] [ i ] ++ ; } } } int main ( ) { int i , j ; clock_t t = clock ( ) ; rowMajor ( ) ; t = clock ( ) - t ; printf ( " Row ▁ major ▁ access ▁ time ▁ : % f ▁ s STRNEWLINE " , t / ( float ) CLOCKS_PER_SEC ) ; t = clock ( ) ; colMajor ( ) ; t = clock ( ) - t ; printf ( " Column ▁ major ▁ access ▁ time ▁ : % f ▁ s STRNEWLINE " , t / ( float ) CLOCKS_PER_SEC ) ; return 0 ; }