![Ubuntu 下的内存速度(预期为 0.17)](https://linux22.com/image/1202007/Ubuntu%20%E4%B8%8B%E7%9A%84%E5%86%85%E5%AD%98%E9%80%9F%E5%BA%A6%EF%BC%88%E9%A2%84%E6%9C%9F%E4%B8%BA%200.17%EF%BC%89.png)
我编写了一个简单的程序来检查内存写入速度(附件),发现我的内存工作速度大约是预期速度的 1/6。
硬件是:联想 Legion 5 pro。处理器:带有 Radeon 显卡的 AMD® Ryzen 7 5800h。
内存:
DDR4-3200 (PC4-25600)
25600 MB/s Peak Transfer Rate
3200 Data transfers/second (in millions)
(3200*8=25600)
写入速度必须为 25.6 字节/纳秒。但我得到的结果是:
$ ./writetest64
testing RAM write speed
writing 8 bytes at a time
total 8192000000 bytes
1842506647 ns
speed: 4.44612 bytes/ns
speed: 4446116931 bytes/s
speed: 4446.12 Mbytes/s
speed: 555.765 M writes/s
done!
那是,556 M 数据传输而不是 3200 M 数据传输。 那是,慢 5.75 倍比标称 RAM 速度(DDR4-3200 为 3200 M 数据传输率/秒)更快。
发生了什么事?内存为什么这么慢?是否应该有某种驱动程序之类的?
我用来测试RAM写入速度的程序:
#include <stdio.h>
#include <iostream>
#include <chrono>
int main() {
printf("testing RAM write speed\n");
int WIDTH = 16000000;
int HEIGHT = 64;
int64_t* target = new int64_t[WIDTH*HEIGHT];
{
int from = 0;
int to = WIDTH*HEIGHT;
for (int j = from; j < to; j++) {
target[j] = 0;
}
}
auto start = std::chrono::high_resolution_clock::now();
{
int from = 0;
int to = WIDTH*HEIGHT;
for (int j = from; j < to; j++) {
target[j] = j*2+1 + ((int64_t)j<<33);
}
}
auto finish = std::chrono::high_resolution_clock::now();
auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(finish-start).count();
long total = WIDTH*HEIGHT*sizeof(target[0]);
std::cout << "writing " << sizeof(target[0]) << " bytes at a time\n";
std::cout << "total "<< total << " bytes\n";
std::cout << nanos << " ns\n";
std::cout << "speed: "<< ((double)total) / nanos << " bytes/ns\n";
std::cout << "speed: "<< (total * 1000'000'000L) / nanos << " bytes/s\n";
std::cout << "speed: "<< ((double)total * 1000L) / nanos << " Mbytes/s\n";
std::cout << "speed: "<< ((double)WIDTH*HEIGHT * 1000L) / nanos << " M writes/s\n";
printf("done!\n");
return 0;
}
汇编:
g++ -O3 writetest64.cpp -o writetest64
GCC 版本:gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
。
万一,
$ sudo lshw -c memory
...
*-cache:0
description: L1 cache
physical id: 5
slot: L1 - Cache
size: 512KiB
capacity: 512KiB
clock: 1GHz (1.0ns)
capabilities: pipeline-burst internal write-back unified
configuration: level=1
*-cache:1
description: L2 cache
physical id: 6
slot: L2 - Cache
size: 4MiB
capacity: 4MiB
clock: 1GHz (1.0ns)
capabilities: pipeline-burst internal write-back unified
configuration: level=2
*-cache:2
description: L3 cache
physical id: 7
slot: L3 - Cache
size: 16MiB
capacity: 16MiB
clock: 1GHz (1.0ns)
capabilities: pipeline-burst internal write-back unified
configuration: level=3
*-memory
description: System Memory
physical id: 23
slot: System board or motherboard
size: 16GiB
*-bank:0
description: SODIMM DDR4 Synchronous Unbuffered (Unregistered) 3200 MHz (0,3 ns)
product: M471A1G44AB0-CWE
vendor: Samsung
physical id: 0
serial: ***
slot: DIMM 0
size: 8GiB
width: 64 bits
clock: 3200MHz (0.3ns)
*-bank:1
description: SODIMM DDR4 Synchronous Unbuffered (Unregistered) 3200 MHz (0,3 ns)
product: M471A1G44AB0-CWE
vendor: Samsung
physical id: 1
serial: ***
slot: DIMM 0
size: 8GiB
width: 64 bits
clock: 3200MHz (0.3ns)
这里看起来奇怪的是clock: 1GHz (1.0ns)
缓存和clock: 3200MHz (0.3ns)
系统内存。
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
$ uname -a
Linux *** 5.15.0-94-generic #104-Ubuntu SMP Tue Jan 9 15:25:40 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux