Most people see arrays like this:
... but arrays are stored as a linear structure of consecutive cells. Arrays look like this in memory and visualizing them might help better understand their performance difference.
public class TestArrays{
public static void main(String[] args){
int[][] array2D=new int[Dim.R.val][Dim.C.val];
long milis = System.currentTimeMillis();
processByRow(array2D);
System.out.println("2D Array
processed by row takes "+(System.currentTimeMillis() - milis)+"
ms");
milis = System.currentTimeMillis();
processByColumn(array2D);
System.out.println("2D Array
processed by column takes "+(System.currentTimeMillis() - milis)+"
ms");
}
private static void processByRow(int[][] array2D){
for(int row=0;row<Dim.R.val;row++){
for(int column=0;column<Dim.C.val;column++){
array2D[row][column]=5;
}
}
}
private static void
processByColumn(int[][] array2D){
for(int column=0;column<Dim.C.val;column++){
for(int row=0;row<Dim.R.val;row++){
array2D[row][column]=5;
}
}
}
private enum Dim{R(10),C(10);
public final int val;
Dim(int val){this.val=val;}
public int getValue() { return val; }
}
}
Run msinfo32 on your system to gather basic info about your basic configuration.
OS Name Microsoft Windows 8.1 Pro
System Type:x64-based PC
Processor:Intel(R) Core(TM) i7-4702HQ CPU @ 2.20GHz, 2201 Mhz, 4 Core(s), 8 Logical Processor(s)
Installed Physical Memory (RAM):16.0 GB
Total Physical Memory:15.9 GB
Available Physical Memory:6.78 GB
Total Virtual Memory:18.3 GB
Available Virtual Memory:8.72 GB
Page File Space:2.38 GB
10x10 Array Processed
2D Array processed by row takes 0 ms
2D Array processed by column takes 0 ms
100x100 Array Processed
2D Array processed by row takes 1 ms
2D Array processed by column takes 0 ms
1000x1000 Array Processed
2D Array processed by row takes 2 ms
2D Array processed by column takes 6 ms
1000x10000 Array Processed
2D Array processed by row takes 17 ms
2D Array processed by column takes 265 ms
10000x1000 Array Processed
2D Array processed by row takes 16 ms
2D Array processed by column takes 136 ms
Run msinfo32 on your system to gather basic info about your basic configuration.
OS Name Microsoft Windows 8.1 Pro
System Type:x64-based PC
Processor:Intel(R) Core(TM) i7-4702HQ CPU @ 2.20GHz, 2201 Mhz, 4 Core(s), 8 Logical Processor(s)
Installed Physical Memory (RAM):16.0 GB
Total Physical Memory:15.9 GB
Available Physical Memory:6.78 GB
Total Virtual Memory:18.3 GB
Available Virtual Memory:8.72 GB
Page File Space:2.38 GB
10x10 Array Processed
2D Array processed by row takes 0 ms
2D Array processed by column takes 0 ms
100x100 Array Processed
2D Array processed by row takes 1 ms
2D Array processed by column takes 0 ms
1000x1000 Array Processed
2D Array processed by row takes 2 ms
2D Array processed by column takes 6 ms
1000x10000 Array Processed
2D Array processed by row takes 17 ms
2D Array processed by column takes 265 ms
10000x1000 Array Processed
2D Array processed by row takes 16 ms
2D Array processed by column takes 136 ms
No comments:
Post a Comment