Question: public class RowvColumn { public static void main(String[] args) { /**This program compares the run-time between row-major and column major * ordering. */ //creating a
public class RowvColumn { public static void main(String[] args) { /**This program compares the run-time between row-major and column major * ordering. */
//creating a 2d Array int[][] twoD = new int[5000][5000];
long start = System.currentTimeMillis(); for (int row = 0 ; row < twoD.length; row++) { for (int col = 0; col < twoD[row].length; col++) { twoD[row][col] = row + col; } } long mid = System.currentTimeMillis(); for (int row = 0; row < twoD.length; row++) { for (int col = 0; col < twoD[row].length; col++) { twoD[col][row] = row + col; } } long end = System.currentTimeMillis(); System.out.println("Speed to traverse Row-Major (Milliseconds): " + (mid - start)); System.out.println("Speed to traverse Column-Major (Milliseconds): " + (end-mid)); } }
Using the example code from Row vs. Column Major, answer the following questions in complete sentences:
- Is Row-Major or Column-Major order faster? Why do you think that is. Please explain using execution counts.
- In what scenario would you use Row-Major ordering when traversing a 2D array? In what scenario would you use Column-Major ordering?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
