测量缓存行大小的简单测试
Simple test to measure cache lines size
从这篇文章开始——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 / 1000 << endl;
return 0;
}
使用各种值作为步骤,我看不到运行时间的跳跃:
step, microseconds
1, 451725
2, 334981
3, 287679
4, 261813
5, 254265
6, 246077
16, 215035
32, 207410
64, 202526
128, 197089
256, 195154
我希望看到类似的东西:
但从16岁开始,每增加一步,运行时间就会减半。
我在Ubuntu13、Xeon X5450上测试了它,并用:g++-O0编译了它。是我的代码有缺陷,还是结果确实不错?如果能对我遗漏的内容有所了解,我们将不胜感激。
因为我看到你想观察缓存线大小的影响,我建议你使用cachegrind工具,它是valgrind工具集的一部分。你的方法是正确的,但并不接近结果。
#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;
}
运行toolvalgrind--tool=cachegrind/a.out$cacheline大小,您应该会看到结果。绘制后,您将准确地获得所需的结果。快乐的实验!!
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);
}
}
}
此代码可帮助您确定一级数据缓存大小。你可以在这里详细阅读。https://medium.com/@behzodbekqodirov/threading-in-java-194b7db6c1de#.kzt4w8eul
相关文章:
- Cmake无法在qt-creater/collect2上编译简单的测试程序:错误:ld
- 为什么这个简单的 C++ SIMD 基准测试在使用 SIMD 指令时运行速度较慢?
- 模型测试 + 简单表模式 = 父测试失败
- 多个 if-else 测试的更简单方法
- 简单的 for() 循环基准测试在任何循环绑定下花费相同的时间
- CMake 和 Visual Studio 2015 - C 编译器 cl.exe 无法编译简单的测试程序
- Centos6 gcc6:编译简单的 c++11 测试文件时,默认的 ABI 不拾取
- C++编译器 g++.exe 无法编译简单的测试程序 - 确定 CXX 编译器是否工作失败
- 为什么我不能编译这个简单的线程测试?
- 测试对象是什么类的简单方法
- CMake 在尝试编译简单测试程序时失败
- 无法运行简单的 std::async 和 std::future 测试程序。错误:"has initializer but incomplete type."发生了什么?
- NAS(使用 qt/linux)- 无法建立简单的音频服务器连接测试
- C 编译器无法编译简单的测试程序
- CL.EXE无法编译简单的测试程序VS2017
- 用谷歌基准测试简单代码的问题
- 简单的MPI_GATHER测试带有memcpy错误
- BoostRegex-在AIX5.3上,简单测试会给出多个ld:0711-224警告
- 测量缓存行大小的简单测试
- 在我的简单测试中,Java在递归算法速度比较上胜过c++