空间局部性

1. 横列赋值(耗时50-60ms)

public class Test {
    public static void main(String[] args) {

        int [] []  arr= new  int[100000][1024];
        long start = System.currentTimeMillis();
        for (int i =0;i<100000;i++){
            for (int j = 0;j<1024;j++){
                arr[i][j] = 0; // 横列赋值,发生100000内存页面读取
            }
        }
        long end  = System.currentTimeMillis();
        System.out.println(end -start);
    }
}

2.竖列赋值(耗时2300-2500ms)

public class Test {
    public static void main(String[] args) {

        int [] []  arr= new  int[100000][1024];
        long start = System.currentTimeMillis();
        for (int i =0;i<1024;i++){
            for (int j = 0;j<100000;j++){
                arr[j][i] = 0; // 竖列赋值,发生100000*4096此内存页读取
            }
        }
        long end  = System.currentTimeMillis();
        System.out.println(end -start);
    }
}

转载于:https://my.oschina.net/Aruforce/blog/3059137

猜你喜欢

转载自blog.csdn.net/weixin_33699914/article/details/91929372