我编写了一个简单的程序(借助 Igor Ostrovsky 的文章“处理器缓存效果图库”),用于检查我的处理器中的缓存级别。根据 Microsoft 提供的 Coreinfo,我的处理器具有以下缓存级别:
+------------------------+---------------+----------+-------------+
| *- Data Cache 0 | Level 1 32 KB | Assoc 8 | LineSize 64 |
| *- Instruction Cache 0 | Level 1 32 KB | Assoc 8 | LineSize 64 |
| -* Data Cache 1 | Level 1 32 KB | Assoc 8 | LineSize 64 |
| -* Instruction Cache 1 | Level 1 32 KB | Assoc 8 | LineSize 64 |
| ** Unified Cache 0 | Level 2 3 MB | Assoc 12 | LineSize 64 |
+------------------------+---------------+----------+-------------+
但如果我运行:
#include <vector>
#include <iostream>
#include <omp.h>
int main(int argc, char* argv[])
{
int sz = 256;
for (size_t s = 0; s < 18; s++)
{
std::vector<int> arr(sz, sz);
int steps = 64 * 1024 * 1024;
double start = omp_get_wtime();
for (int i = 0; i < steps; i++)
{
arr[(i * 16) % arr.size()]++;
}
double end = omp_get_wtime();
std::cout << "size: " << (4 * sz) / 1024 << "KB\t\t";
std::cout << "avg time: " << (end - start)*1000000000 / steps << "ns" << std::endl;
sz *= 2;
}
}
我得到以下输出:
size: 1KB avg time: 4.36752ns
size: 2KB avg time: 4.27807ns
size: 4KB avg time: 4.19745ns
size: 8KB avg time: 3.93159ns
size: 16KB avg time: 3.95425ns
size: 32KB avg time: 3.88134ns
size: 64KB avg time: 3.8112ns
size: 128KB avg time: 3.60893ns
size: 256KB avg time: 3.61895ns
size: 512KB avg time: 3.62264ns
size: 1024KB avg time: 3.63614ns
size: 2048KB avg time: 4.34382ns
size: 4096KB avg time: 24.5113ns
size: 8192KB avg time: 28.271ns
size: 16384KB avg time: 28.1093ns
size: 32768KB avg time: 28.3462ns
size: 65536KB avg time: 28.2305ns
size: 131072KB avg time: 28.4871ns
由于 L2 3MB 缓存,2048KB 和 4096KB 之间的访问时间明显增加。但是为什么由于 L1 32KB 缓存,32KB 和 64KB 之间的访问时间没有出现类似的增加?我的处理器规格:关联