实测顺序访存VS随机访存,探究cache命中的作用有多大
程序员文章站
2022-03-11 16:16:37
...
结论1:一般CPU是64字节/cache行;所以顺序访存比随机访存快30倍,较为合理;
结论2: 访存期间,CPU也是满的;
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <chrono>
using namespace std::chrono;
// 结论1:一般CPU是64字节/cache行;所以顺序访存比随机方寸快30倍,较为合理;
// 结论2: 访存期间,CPU也是满的;
//int main()
//{
// const int MemSize = 1024 * 1024 * 256;
// char* mem = new char[MemSize];
// char result = 0;
// uint64_t count = 0;
// auto start = system_clock::now();
// while (true) {// Sequential & no-rand(), char: 1.5GB/s
// int offset = (rand() * RAND_MAX + rand()) & ((1 << 28) - 1);
// int mem_offset = count & ((1 << 28) - 1);
// result ^= mem[mem_offset];
// ++count;
// if (offset < 10) {
// auto end = system_clock::now();
// auto duration = end - start;
// double seconds = (double) (duration_cast<std::chrono::milliseconds>(duration).count()) / 1000;
// double speed = (double)count / seconds;
// printf("%d_%lf_%lf\t", result, speed, seconds);
// }
// }
// std::cout << "Hello World!\n";
//}
typedef int DataType;
/*
int main()
{// 顺序byte内存,char:1.3GB/s; int:4.7GB/s
const int MemSize = 1024 * 1024 * 64;
DataType* mem = new DataType[MemSize];
int* ids = new int[MemSize];
for (int i = 0; i < MemSize; i += 1) {
ids[i] = i;
}
printf("Begin\n");
DataType result = 0;
uint64_t count = 0;
auto start = system_clock::now();
while (true) {
int round_count = count & ((1 << 26) - 1);
int offset = ids[round_count];
result ^= mem[offset];
++count;
if (round_count == 0) {
auto end = system_clock::now();
auto duration = end - start;
double seconds = (double)(duration_cast<std::chrono::milliseconds>(duration).count()) / 1000;
double speed = (double)count * sizeof(DataType) / seconds;
printf("%d_%lf_%lf\t", result, speed, seconds);
}
}
std::cout << "Hello World!\n";
}
*/
int main()
{// 随机访问byte内存,char:44MB/s; int:167MB/s;
const int MemSize = 1024 * 1024 * 64;
DataType* mem = new DataType[MemSize];
int* ids = new int[MemSize];
for (int i = 0; i < MemSize; i += 1) {
ids[i] = (rand() * RAND_MAX + rand()) & ((1 << 26) - 1);
}
printf("Begin\n");
DataType result = 0;
uint64_t count = 0;
auto start = system_clock::now();
while (true) {
int round_count = count & ((1 << 26) - 1);
int offset = ids[round_count];
result ^= mem[offset];
++count;
if (round_count == 0) {
auto end = system_clock::now();
auto duration = end - start;
double seconds = (double)(duration_cast<std::chrono::milliseconds>(duration).count()) / 1000;
double speed = (double)count * sizeof(DataType) / seconds;
printf("%d_%lf_%lf\t", result, speed, seconds);
}
}
std::cout << "Hello World!\n";
}