In Java, a 2D array is created using the following parameters: int A[][] = new i
ID: 3808905 • Letter: I
Question
In Java, a 2D array is created using the following parameters:
int A[][] = new int[row][column];
Compare the following two code segments which access this array. Do you think they are equivalent in performance? Specify how you investigate this problem, and show the results of your experiments.
Code segment 1
Code segment 2
for (i=0; i<row; i++) {
for (j=0; j<column; j++) {
A[i][j] = i*column+j;
}
}
for (j=0; j<column; j++) {
for (i=0; i<row; i++) {
A[i][j] = i*column+j;
}
}
Read the article “Memory layout of multi-dimensional arrays” by Bendersky (http://eli.thegreenplace.net/2015/memory-layout-of-multi-dimensional-arrays/).
needed for the question:
a. Correctness of experimental setup (code required)
b. Thoroughness of coverage (see possible test sets)
c. Correctness of deduction
Sample test sets (you may try them, or design you own sets):
Square
arrays
row
column
Time use for segment 1
Time use for segment 2
10
10
100
100
1000
1000
10000
10000
Code segment 1
Code segment 2
for (i=0; i<row; i++) {
for (j=0; j<column; j++) {
A[i][j] = i*column+j;
}
}
for (j=0; j<column; j++) {
for (i=0; i<row; i++) {
A[i][j] = i*column+j;
}
}
Explanation / Answer
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99
Total Time of execution Code Segment 1: 0
0 10 20 30 40 50 60 70 80 90
1 11 21 31 41 51 61 71 81 91
2 12 22 32 42 52 62 72 82 92
3 13 23 33 43 53 63 73 83 93
4 14 24 34 44 54 64 74 84 94
5 15 25 35 45 55 65 75 85 95
6 16 26 36 46 56 66 76 86 96
7 17 27 37 47 57 67 77 87 97
8 18 28 38 48 58 68 78 88 98
9 19 29 39 49 59 69 79 89 99
Total Time of execution Code Segment 2: 0
Result:
Rows : 10 Columns : 10
Total Time of execution Code Segment 1: 0
Total Time of execution Code Segment 2: 0
Rows : 100 Columns : 100
Total Time of execution Code Segment 1: 0
Total Time of execution Code Segment 2: 0
Rows : 1000 Columns : 1000
Total Time of execution Code Segment 1: 16
Total Time of execution Code Segment 2: 15
Rows : 10000 Columns : 10000
Total Time of execution Code Segment 1: 187
Total Time of execution Code Segment 2: 4979
Code:
public class CodeSegment1 {
public static void main(String str[]){
int i,j,column,row;
for(int p=1;p<=4;p++){
row=(int)Math.pow(10,p);
column=(int)Math.pow(10,p);
int [][]A=new int[row][column];
long startTime = System.currentTimeMillis();
for (i=0; i<row; i++) {
for (j=0; j<column; j++) {
A[i][j] = i*column+j;
//System.out.print(A[i][j]+" ");
}
//System.out.println("");
}
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println("Rows : "+row + " Columns : "+column+" ");
System.out.println("Total Time of execution Code Segment 1: "+totalTime+" ");
startTime = System.currentTimeMillis();
for (j=0; j<column; j++) {
for (i=0; i<row; i++) {
A[i][j] = i*column+j;
//System.out.print(A[i][j]+" ");
}
//System.out.println("");
}
endTime = System.currentTimeMillis();
totalTime = endTime - startTime;
System.out.println("Total Time of execution Code Segment 2: "+totalTime+" ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.