这个实验主要是将高速缓存命中的一点东西,意在告诉我们平常多注意这方面的东西。
不懂java的,所以只管C的部分。
You will do this several times, making small modifications to see what differences they make—how the choice of language affects performance and how effective the compiler can be at optimizing your code when you:
- interchange the order of the
iandjloops - uncomment the commented line
- change the size of the array being copied from 2048 x 2048 to 4096 x 4096
#include <stdio.h>
int src[2048][2048];
int dst[2048][2048];
/* Copies the contents of one 2048-by-2048 array (src) into another (dst). */
int main(int argc, char* argv[])
{
// declare all variables before the code (conform to an older C standard...)
int rep;
int i, j;
for ( rep = 0; rep < 10; rep++ )
{
for ( i= 0; i < 2048;i++ )
{
for ( j= 0; j< 2048; j++ )
{
//src[i][j] = i * rep;
dst[i][j] = src[i][j];
}
}
}
return 0;
}
| 不优化 | O2优化 | |
| 原始程序 | 0.163 | 0.167 |
| 调换i,j顺序 | 1.332 | 1.305 |
| 加上src数组赋值 | 1.814 | 1.815 |
| 改变数组大小为4096 | 8.424 | 8.487 |
上诉结果都是在保持上一个改变的基础上得出的结果,也就是加上src数组赋值这一步骤时,i和j的顺序还是调换的。
- What are the source code differences among the two Java implementations?
由于不懂java,所以只能编译一下C的程序,对java的就暂时不管了。
2.Pick a single pair of results that most surprised you. What is it about the results that surprised you?

本文探讨了高速缓存对程序性能的影响,通过C语言实验展示了循环顺序、数组大小变化以及优化选项如何改变结果。实验表明,GCC的-O2优化并不总是提升性能,甚至可能导致性能下降。同时,文章详细解析了如何推断神秘的高速缓存几何特性,包括缓存块大小、总容量和高速缓存行大小的计算方法。
2467

被折叠的 条评论
为什么被折叠?



