Row | Java 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
import java . time . Duration ; import java . time . Instant ; import java . util . * ; class GFG { static int MAX = 10000 ; static int [ ] [ ] arr = new int [ MAX ] [ MAX ] ; static void rowMajor ( ) { int i , j ; for ( i = 0 ; i < MAX ; i ++ ) { for ( j = 0 ; j < MAX ; j ++ ) { arr [ i ] [ j ] ++ ; } } } static void colMajor ( ) { int i , j ; for ( i = 0 ; i < MAX ; i ++ ) { for ( j = 0 ; j < MAX ; j ++ ) { arr [ j ] [ i ] ++ ; } } } public static void main ( String [ ] args ) { Instant start = Instant . now ( ) ; rowMajor ( ) ; Instant end = Instant . now ( ) ; System . out . println ( " Row ▁ major ▁ access ▁ time ▁ : ▁ " + Duration . between ( start , end ) ) ; start = Instant . now ( ) ; colMajor ( ) ; end = Instant . now ( ) ; System . out . printf ( " Column ▁ major ▁ access ▁ time ▁ : ▁ " + Duration . between ( start , end ) ) ; } }