######################################## Cache ######################################## Cache 对循环的影响 **************************************** CPU 在读取 Cache 的时候一次读取一个 Cache Line。Cache Line 常见的大小为 64 字节。在循环中步长在一个 Cache Line 中的循环对速度影响一般不大,但是对于不同行的 Cache Line 具有很大的变化。例如: .. code-block:: java static public void main(String [] args){ int [] arr = new int[64*1024*1024]; long start1 = System.currentTimeMillis(); for(int i =0; i result = end - start; cout << result.count() << endl; }; printTime(1); printTime(2); printTime(6); printTime(10); printTime(16); printTime(32); 结果是:: 0.177454 0.0971365 0.0398913 0.0338586 0.028566 0.0274254 这里和 Java 明显不同。这已经是关闭了编译器优化了。即使是清空流水线和清除 cache 也不会影响倍数关系。在我电脑上一级和二级缓存的 Cache Line 都是 64 字节