Some surprising results…
public class CacheGrid {
private static final int dim = (1 << 12);
private static final int[] grid = new int[dim * dim];
private static final int[][] rows = new int[dim][dim];
public static void main(String[] args) {
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("java.vm.version"));
for(int i = 0; i < 10; i++) {
System.out.println();
{
long t0 = System.nanoTime();
fill_1d_XY();
long t1 = System.nanoTime();
System.out.println("fill_1d_XY took: " + (t1 - t0) / 1000000 + "ms");
}
{
long t0 = System.nanoTime();
fill_1d_YX();
long t1 = System.nanoTime();
System.out.println("fill_1d_YX took: " + (t1 - t0) / 1000000 + "ms");
}
{
long t0 = System.nanoTime();
fill_2d_XY();
long t1 = System.nanoTime();
System.out.println("fill_2d_XY took: " + (t1 - t0) / 1000000 + "ms");
}
{
long t0 = System.nanoTime();
fill_2d_YX();
long t1 = System.nanoTime();
System.out.println("fill_2d_YX took: " + (t1 - t0) / 1000000 + "ms");
}
}
}
public static void fill_1d_XY() {
for(int x = 0; x < dim; x++)
for(int y = 0; y < dim; y++)
grid[(y << 12) | x] = y * dim + x;
}
public static void fill_1d_YX() {
for(int y = 0; y < dim; y++)
for(int x = 0; x < dim; x++)
grid[(y << 12) | x] = y * dim + x;
}
public static void fill_2d_XY() {
for(int x = 0; x < dim; x++)
for(int y = 0; y < dim; y++)
rows[y][x] = y * dim + x;
}
public static void fill_2d_YX() {
for(int y = 0; y < dim; y++)
for(int x = 0; x < dim; x++)
rows[y][x] = y * dim + x;
}
}
1.7.0_45
24.45-b08
fill_1d_XY took: 1229ms
fill_1d_YX took: 20ms
fill_2d_XY took: 545ms
fill_2d_YX took: 20ms
fill_1d_XY took: 1227ms
fill_1d_YX took: 16ms
fill_2d_XY took: 539ms
fill_2d_YX took: 15ms
fill_1d_XY took: 1225ms
fill_1d_YX took: 17ms
fill_2d_XY took: 539ms
fill_2d_YX took: 15ms
fill_1d_XY took: 1225ms
fill_1d_YX took: 17ms
fill_2d_XY took: 539ms
fill_2d_YX took: 15ms
fill_1d_XY took: 1229ms
fill_1d_YX took: 17ms
fill_2d_XY took: 538ms
fill_2d_YX took: 15ms
fill_1d_XY took: 1223ms
fill_1d_YX took: 17ms
fill_2d_XY took: 538ms
fill_2d_YX took: 15ms
In this micro-benchmark, int[w][h] is always faster than int[w*h], who would-a-thunk!
(Probably due to HotSpot not able to remove bounds-checks in the 1D version)