public void testtt() { int [][] array = new int [64 * 1024][1024 ]; // 横向遍历 long currentTimeMillis = System.currentTimeMillis(); for ( int i = 0; i 64 * 1024; i ++ ) for ( int j = 0; j 1024; j ++ ) array[i][j] ++ ; System.out.printl
public void testtt() { int[][] array = new int[64 * 1024][1024]; // 横向遍历 long currentTimeMillis = System.currentTimeMillis(); for(int i = 0; i < 64 * 1024; i ++) for(int j = 0; j < 1024; j ++) array[i][j] ++; System.out.println(System.currentTimeMillis()-currentTimeMillis); // 纵向遍历 currentTimeMillis = System.currentTimeMillis(); for(int i = 0; i < 1024; i ++) for(int j = 0; j < 64 * 1024; j ++) array[j][i] ++; System.out.println(System.currentTimeMillis()-currentTimeMillis); }
这里涉及到cpu cache命中率的问题,横向遍历的命中率大于纵向遍历
附详细地址:https://blog.csdn.net/u010983881/article/details/82704733