【发布时间】:2016-10-18 11:34:26
【问题描述】:
我正在测试两个几乎相同的代码,其中一个 for 循环略有不同。第一个使用三个循环迭代索引y,z,x,而第二个迭代x,z,y。
我的问题是为什么用户时间和挂钟时间不同?是因为一个代码和另一个代码中的内存位置吗?
test_1.c:
#define N 1000
// Matrix definition
long long int A[N][N],B[N][N],R[N][N];
int main()
{
int x,y,z;
char str[100];
/*Matrix initialization*/
for(y=0;y<N;y++)
for(x=0;x<N;x++)
{
A[y][x]=x;
B[y][x]=y;
R[y][x]=0;
}
/*Matrix multiplication*/
for(y=0;y<N;y++)
for(z=0;z<N;z++)
for(x=0;x<N;x++)
{
R[y][x]+= A[y][z] * B[z][x];
}
exit(0);
}
第二个代码 (test_2.c) 的区别在于最后一个 for 循环:
for(x=0;x<N;x++)
for(z=0;z<N;z++)
for(y=0;y<N;y++)
{
R[y][x]+= A[y][z] * B[z][x];
}
如果我打印 /user/bin/time -v ./test_1 我会得到以下统计信息:
Command being timed: "./test_1"
User time (seconds): 5.19
System time (seconds): 0.01
Percent of CPU this job got: 99%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:05.22
而 /user/bin/time -v ./test_2 提供以下统计信息:
Command being timed: "./test_2"
User time (seconds): 7.75
System time (seconds): 0.00
Percent of CPU this job got: 99%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:07.76
【问题讨论】:
-
这可能是因为您使用一种方法比使用另一种方法获得更多的缓存未命中。谷歌“缓存数据本地化”。
-
尝试使用缓存分析工具(如
cachegrind)运行您的代码。
标签: c