这个实验主要是将高速缓存命中的一点东西,意在告诉我们平常多注意这方面的东西。
不懂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
i
andj
loops - 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? (That is, from the 32 pairs of measurement results, pick one pair whose relationship is least like what you would have guessed.)<