从这篇文章开始--Igor Ostrovsky 的处理器缓存效果库 - 我想在我自己的机器上玩他的例子.这是我的第一个示例的代码,它查看不同缓存行如何影响运行时间:
#include <iostream>
#include <time.h>
using namespace std;
int main(int argc, char* argv[])
{
int step = 1;
const int length = 64 * 1024 * 1024;
int* arr = new int[length];
timespec t0, t1;
clock_gettime(CLOCK_REALTIME, &t0);
for (int i = 0; i < length; i += step)
arr[i] *= 3;
clock_gettime(CLOCK_REALTIME, &t1);
long int duration = (t1.tv_nsec - t0.tv_nsec);
if (duration < 0)
duration = 1000000000 + duration;
cout<< step << ", " << duration …Run Code Online (Sandbox Code Playgroud)