【发布时间】:2015-09-21 15:53:45
【问题描述】:
我有一台配备 Intel Core 2 Duo 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/秒,但我得到了以下结果:
17530212352 bytes transferred in 10.0 secs (1671.811328125 MiB/sec)
31237926912 bytes transferred in 10.0 secs (2979.080859375 MiB/sec)
速度接近 3 GiB/秒怎么可能?
【问题讨论】:
-
你忘记了 CPU 缓存。有 l1、l2 甚至 l3 缓存......仅仅因为你在随机浏览并不意味着你不会偶尔在缓存中获得命中。
-
@MarcB 是的。这就是我创建 256MiB 缓冲存储的原因。
-
DDRx 内存通常也是 64 位宽。仅仅因为它以 1066mhz 运行并不意味着它是 1byte/hz 传输速率......
-
您对DDR频率的理解从根本上是错误的。看看en.wikipedia.org/wiki/DDR_SDRAM。一般来说,您的人数很少。
-
对于初学者来说,除了执行复制之外,您还要执行代码,这会使您的测量从一开始就不准确。
标签: java performance memory hardware benchmarking