[英]Simple test to measure cache lines size
Starting from this article - Gallery of Processor Cache Effects by Igor Ostrovsky - I wanted to play with his examples on my own machine. 从这篇文章开始--Igor Ostrovsky 的处理器缓存效果库 - 我想在我自己的机器上玩他的例子。 This is my code for the first example, that looks at how touching different cache lines affect running time: 这是我的第一个示例的代码,它查看不同缓存行如何影响运行时间:
#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 / 1000 << endl;
return 0;
}
Using various values for step, I don't see the jump in the running time: 使用步骤的各种值,我看不到运行时间的跳跃:
step, microseconds
1, 451725
2, 334981
3, 287679
4, 261813
5, 254265
6, 246077
16, 215035
32, 207410
64, 202526
128, 197089
256, 195154
I would expect to see something similar with: 我希望看到类似的东西:
But from 16 onwards, the running time is halved each time we double the step. 但是从16开始,每当我们加倍步时,运行时间减半。
I test it on an Ubuntu13, Xeon X5450 and compiling it with: g++ -O0. 我在Ubuntu13,Xeon X5450上测试它并用以下代码编译:g ++ -O0。 Is something flawed with my code, or the results are actually ok? 我的代码有什么问题,或者结果确实没问题? Any insight on what I'm missing would be highly appreciated. 任何关于我缺少的东西的见解都将受到高度赞赏。
As i see you want to observe effect of cache line sizes, i recommend tool cachegrind, part of valgrind tool set. 因为我看到你想观察缓存行大小的影响,我推荐工具cachegrind,valgrind工具集的一部分。 Your approach is right but not close to results. 你的方法是对的,但不接近结果。
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main(int argc, char* argv[])
{
int step = atoi(argv[1]);
const int length = 64 * 1024 * 1024;
int* arr = new int[length];
for (int i = 0; i < length; i += step)
arr[i] *= 3;
return 0;
}
Run tool valgrind --tool=cachegrind ./a.out $cacheline-size and you should see results. 运行工具valgrind --tool = cachegrind ./a.out $ cacheline-size ,你应该看到结果。 After plotting this you will get desired results with accuracy. 绘制完成后,您将获得准确的预期结果。 Happy Experimenting!! 快乐实验!!
public class CacheLine {
public static void main(String[] args) {
CacheLine cacheLine = new CacheLine();
cacheLine.startTesting();
}
private void startTesting() {
byte[] array = new byte[128 * 1024];
for (int testIndex = 0; testIndex < 10; testIndex++) {
testMethod(array);
System.out.println("--------- // ---------");
}
}
private void testMethod(byte[] array) {
for (int len = 8192; len <= array.length; len += 8192) {
long t0 = System.nanoTime();
for (int i = 0; i < 10000; i++) {
for (int k = 0; k < len; k += 64) {
array[k] = 1;
}
}
long dT = System.nanoTime() - t0;
System.out.println("len: " + len / 1024 + " dT: " + dT + " dT/stepCount: " + (dT) / len);
}
}
}
This code helps you with determining L1 data cache size. 此代码可帮助您确定L1数据高速缓存大小。 You can read about it more in detail here. 您可以在此处详细了解它。 https://medium.com/@behzodbekqodirov/threading-in-java-194b7db6c1de#.kzt4w8eul https://medium.com/@behzodbekqodirov/threading-in-java-194b7db6c1de#.kzt4w8eul
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.