/* My output: 25037 13204 13796 */ public class MatrixTest { public int width; public int height; public int m[]; public MatrixTest(int w, int h) { this.width = w; this.height = h; this.m = new int[w * h]; } public int get(int x, int y) { return this.m[y * width + x]; } public void set(int x, int y, int value) { this.m[y * width + x] = value; } public static void main (String[] args) { int w = 1000, h = 2000, passes = 400; int matrix[][] = new int[h][]; for (int i = 0; i < h; ++i) { matrix[i] = new int[w]; } long start; start = System.currentTimeMillis(); // X and Y are inverted. for (int z = 0; z < passes; z++) { for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { matrix[y][x] = matrix[y][x] + 1; } } } System.out.println(System.currentTimeMillis() - start); // MatrixTest mt = new MatrixTest(w, h); start = System.currentTimeMillis(); for (int z = 0; z < passes; z++) { for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { mt.set(x, y, mt.get(x, y) + 1); } } } System.out.println(System.currentTimeMillis() - start); // MatrixTest mt2 = new MatrixTest(w, h); start = System.currentTimeMillis(); for (int z = 0; z < passes; z++) { for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { mt2.m[y * w + x] = mt2.m[y * w + x] + 1; } } } System.out.println(System.currentTimeMillis() - start); } }