我不知道为什么 test1 和 test2 在 CPU 使用率上有很大的不同。'top' 在运行 test1 时报告 90% 的 cpu 使用率,而在运行 test2 时报告 100%。test1 的缓冲区大小只有 1KB 以适合我的 L1 缓存,所以它不像缓存未命中问题,我在每个源代码后附上 cachegrind 报告,最后是 env。
真的让我很困惑,欢迎任何意见。
-----测试1-----------
const static int BUFFER_LENGTH=1024;
int main(int argc, char* argv[])
{
char buff[BUFFER_LENGTH];
for(uint64_t i =0; i < 100000000; ++i)
buff[(i*1)%BUFFER_LENGTH] = '1';
return 1;
}
==4813== I refs: 601,353,862
==4813== I1 misses: 1,032
==4813== LLi misses: 1,007
==4813== I1 miss rate: 0.00%
==4813== LLi miss rate: 0.00%
==4813==
==4813== D refs: 400,455,090 (300,341,408 rd + 100,113,682 wr)
==4813== D1 misses: 8,184 ( 6,978 rd + 1,206 wr)
==4813== LLd misses: 4,970 ( 4,047 rd + 923 wr)
==4813== D1 miss rate: 0.0% ( 0.0% + 0.0% )
==4813== LLd miss rate: 0.0% ( 0.0% + 0.0% )
==4813==
==4813== LL refs: 9,216 ( 8,010 rd + 1,206 wr)
==4813== LL misses: 5,977 ( 5,054 rd + 923 wr)
==4813== LL miss rate: 0.0% ( 0.0% + 0.0% )
-----测试2------------
int main(int argc, char* argv[])
{
for(uint64_t i = 0, a = 0; i < 100000000; ++i)
a++;
return 1;
}
==22081== I refs: 401,352,490
==22081== I1 misses: 1,010
==22081== LLi misses: 989
==22081== I1 miss rate: 0.00%
==22081== LLi miss rate: 0.00%
==22081==
==22081== D refs: 300,454,488 (300,340,997 rd + 113,491 wr)
==22081== D1 misses: 8,162 ( 6,966 rd + 1,196 wr)
==22081== LLd misses: 4,965 ( 4,043 rd + 922 wr)
==22081== D1 miss rate: 0.0% ( 0.0% + 1.0% )
==22081== LLd miss rate: 0.0% ( 0.0% + 0.8% )
==22081==
==22081== LL refs: 9,172 ( 7,976 rd + 1,196 wr)
==22081== LL misses: 5,954 ( 5,032 rd + 922 wr)
==22081== LL miss rate: 0.0% ( 0.0% + 0.8% )
测试环境:
CPU: Intel(R) Xeon(R) CPU E5606 @ 2.13GHz (4 core*2)
内存:60G
操作系统:Linux 版本 2.6.18-164.el5(gcc 版本 4.1.2 20080704(Red Hat 4.1.2-46))#1 SMP Tue Aug 18 15:51:48 EDT 2009