CPU中读操作有乱序的情况,其实写操作也有乱序的情况。
设计到CPU的合并写操作。
在CPU的寄存器和L1缓存之间有一个WCBuffer,有四个位置大小。数据读写先存入WCBuffer,然后再在寄存器和L1之间交换。
问题是,现在有长度为6的数据,是一次性操作6个比较快,还是分成两组,一组3个,每组再凑一个字节变成4个长度 的方式快。
实验验证,刚开始分组的形式快,后来就不一定了。
/** * @version v1.0 * @Description 验证CPU 合并写操作。其中 WCBuffer只有4个位置大小。 * 实验验证:CPU一次改6个位置快,还是把6个位置分成两份,都凑成两个4个位置快 */ public class WriteCombining { private static final int ITERATIONS = Integer.MAX_VALUE; private static final int ITEMS = 1 << 24; private static final int MASK = ITEMS - 1; private static final byte[] arrayA = new byte[ITEMS]; private static final byte[] arrayB = new byte[ITEMS]; private static final byte[] arrayC = new byte[ITEMS]; private static final byte[] arrayD = new byte[ITEMS]; private static final byte[] arrayE = new byte[ITEMS]; private static final byte[] arrayF = new byte[ITEMS]; public static void main(final String[] args){ for (int i = 0; i < 5; i++) { System.out.println(i+" SingleLoop duraption (ns) = " +runCaseOne()); System.out.println(i+" SingleLoop duraption (ns) = " +runCaseTwo()); } } public static long runCaseOne(){ long start = System.nanoTime(); int i = ITERATIONS; //第一个循环,尝试一次改动6个位置 while (--i != 0){ int slot = i & MASK; byte b = (byte) i; //b也占用一个位置 arrayA[slot] = b; arrayB[slot] = b; arrayC[slot] = b; arrayD[slot] = b; arrayE[slot] = b; arrayF[slot] = b; } return System.nanoTime() - start; } public static long runCaseTwo(){ long start = System.nanoTime(); int i = ITERATIONS; //分成两个循环,一个循环负责改其中的3个位置 while (--i != 0){ int slot = i & MASK; byte b = (byte) i; //b也占用一个位置,与这三个位置,共同构成4个位置 arrayA[slot] = b; arrayB[slot] = b; arrayC[slot] = b; } while (--i != 0){ int slot = i & MASK; byte b = (byte) i; arrayA[slot] = b; arrayB[slot] = b; arrayC[slot] = b; } return System.nanoTime() - start; } }