實測順序訪存VS隨機訪存,探究cache命中的作用有多大

結論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"; 
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章