首页 > 解决方案 > 缓存似乎无效

问题描述

我要测试Cache的作用?我发现缓存似乎没有效果。

unsigned time1, time2;
int num=1;
int junk=0;
_mm_clflush(&num);
time1 = __rdtscp((unsigned int*)&junk);
num=2;
time2 = __rdtscp((unsigned int*)&junk) ;
printf("%u\n",(time2-time1));

此代码的输出为 59。

unsigned time1, time2;
int num=1;
int junk=0;
//_mm_clflush(&num);
num=3;
time1 = __rdtscp((unsigned int*)&junk);
num=2;
time2 = __rdtscp((unsigned int*)&junk) ;
printf("%u\n",(time2-time1));

这段代码的输出也是59。第一个代码中的num变量不在缓存中,而第二个在缓存中。为什么时间一样?

标签: cperformancex86cpu-cachemicrobenchmark

解决方案


推荐阅读