橫向遍歷比縱向遍歷更高效

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命中率的問題,橫向遍歷的命中率大於縱向遍歷spa

附詳細地址:https://blog.csdn.net/u010983881/article/details/82704733.net

相關文章
相關標籤/搜索