-
-
Save ted-wq-x/899e228df792baf74f51cd3482bb8401 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CacheLineEffect { | |
//考虑一般缓存行大小是64字节,一个 long 类型占8字节 | |
static long[][] arr; | |
public static void main(String[] args) { | |
arr = new long[1024 * 1024][]; | |
for (int i = 0; i < 1024 * 1024; i++) { | |
arr[i] = new long[8]; | |
for (int j = 0; j < 8; j++) { | |
arr[i][j] = 0L; | |
} | |
} | |
long sum = 0L; | |
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.currentTimeMillis() - marked) + "ms"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment