繁体   English   中英

缓存未命中似乎工作不正常

[英]Cache misses seem work not properly

我想用这个简单的代码检查缓存未命中,尝试分配例如包含3个元素的数组,然后分配包含30万个元素的数组,但是在两种情况下用数组元素做某事的时间都相当均匀。

#include <iostream>
#include <cstdlib>
#include <ctime>


int main(int argc, char* argv[]) {
    const int TAB_SIZE = atoi(argv[1]);
    const int TEST_LEN = atoi(argv[2]);

    srand(time(NULL));

    int *tab = new int [TAB_SIZE];

    for(int i=0; i<TEST_LEN;++i) {
        int index = rand()%TAB_SIZE;
        // do something with random indexed array element
        tab[index] = index;
    }

    return 0;
}

这是我的3元素数组的输出:

marc@E540 ~/projects/simple/cache_test $ time ./a.out 3 100000000

real    0m1.236s
user    0m1.232s
sys     0m0.004s

对于30万元素阵列:

marc@E540 ~/projects/simple/cache_test $ time ./a.out 300000 100000000

real    0m1.375s
user    0m1.372s
sys     0m0.000s

第一个数组适合我的缓存,第二个数组不适合:

L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              3072K

当整个数组都适合缓存时,不是应该更快吗?或者差异不是那么大? 有更有效的方法来测试缓存吗?

对于我与gcc的centos

[paul@pmcent work]$ time ./a.out 3 100000000

real    0m1.622s
user    0m1.603s
sys 0m0.000s
[paul@pmcent work]$ time ./a.out 300000 100000000

real    0m2.044s
user    0m2.023s
sys 0m0.000s
[paul@pmcent work]$ 

不是答案,但对评论来说太大了

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM