java-CPU Cache 与缓存行 转
【推荐阅读】微服务还能火多久?>>> 出处: Java编程如何高效利用CPU缓存? 引言 首先我们来看一个Java的例子: public class Main { static long [][] arr; public static void main(String[] args) { arr = new long [1024 * 1024][8 ]; // 横向遍历 long marked = System.currentTimeMillis(); for ( int i = 0; i < 1024 * 1024; i += 1 ) { for ( int j = 0; j < 8; j++ ) { sum += arr[i][j]; } } System.out.println( "Loop times:" + (System.currentTimeMillis() - marked)+ "ms" ); marked = System.currentTimeMillis(); // 纵向遍历 for ( int i = 0; i < 8; i += 1 ) { for ( int j = 0; j < 1024 * 1024; j++ ) { sum += arr[j][i]; } } System.out.println( "Loop times:" + (System