程序超过理论内存传输速率
我有一台配备英特尔酷睿2双核2.4GHz CPU和2x4Gb DDR3模块1066MHz的笔记本电脑。
我期望这个内存可以以1067 MiB / sec的速度运行,只要有两个通道,最大速度为2134 MiB / sec(以防操作系统内存分派器允许)。
我做了一个小的Java应用程序来测试它:
private static final int size = 256 * 1024 * 1024; // 256 Mb
private static final byte[] storage = new byte[size];
private static final int s = 1024; // 1Kb
private static final int duration = 10; // 10sec
public static void main(String[] args) {
long start = System.currentTimeMillis();
Random rnd = new Random();
byte[] buf1 = new byte[s];
rnd.nextBytes(buf1);
long count = 0;
while (System.currentTimeMillis() - start < duration * 1000) {
long begin = (long) (rnd.nextDouble() * (size - s));
System.arraycopy(buf1, 0, storage, (int) begin, s);
++count;
}
double totalSeconds = (System.currentTimeMillis() - start) / 1000.0;
double speed = count * s / totalSeconds / 1024 / 1024;
System.out.println(count * s + " bytes transferred in " + totalSeconds + " secs (" + speed + " MiB/sec)");
byte[] buf2 = new byte[s];
count = 0;
start = System.currentTimeMillis();
while (System.currentTimeMillis() - start < duration * 1000) {
long begin = (long) (rnd.nextDouble() * (size - s));
System.arraycopy(storage, (int) begin, buf2, 0, s);
Arrays.fill(buf2, (byte) 0);
++count;
}
totalSeconds = (System.currentTimeMillis() - start) / 1000.0;
speed = count * s / totalSeconds / 1024 / 1024;
System.out.println(count * s + " bytes transferred in " + totalSeconds + " secs (" + speed + " MiB/sec)");
}
我预计结果低于2134 MiB / sec,但是我得到了以下内容:
17530212352 bytes transferred in 10.0 secs (1671.811328125 MiB/sec)
31237926912 bytes transferred in 10.0 secs (2979.080859375 MiB/sec)
速度几乎达到3 GiB/秒,这怎么可能呢?