0

我正在编写一个程序来确定二级缓存行的大小,我使用了文章http://igoro.com/archive/gallery-of-processor-cache-effects/。但我有完全不同的结果。通过Coreinfo程序得知,一级和二级字符串的大小均为64字节。虽然我决定至少得到第一级线的大小,但是得到了一些完全不充分的结果

#include "stdafx.h"
#include <time.h>
#include <iostream>
#include <string>


using namespace std;


int main()
{
    int t;
    const int N = 8000;
    volatile int arr[N];
    unsigned int A;
    char ask1 = 'y';

    srand(time(NULL));

    while (ask1 == 'y')
    {
        for (int j = 0; j < N; j++)
            arr[j] = rand();

        for (int i = 1; i <= 64; i++)
        {
            A = clock();
            for (int k = 0; k < 100000; k++)
                for (int j = 0; j < N; j += i)
                {
                    t = arr[j];
                }
            //cout << i << "\tstep, time\t" << clock() - A << '\t' << t << endl;
            arr[i] = (clock() - A); //Instead of printing, so as not to destroy the cache, I decided to write the execution time in the same array, and then output it
        }
        for (int i = 1; i <= 64; i++)
            cout << i << "\tstep, time\t" << arr[i] << endl;
        cout << "Repeat?(y/n): ";
        cin >> ask1;
        cout << endl;
    }
    /**/


    const int n = 1600000000;

    int l;

    unsigned int a;
    char ask = 'y';
    srand(time(NULL)); //it's just for random filling, so it's always been different, although here it's not really necessary for me
    while (ask == 'y')
    {
        volatile int byte8[2];

        for (int j = 0; j < 2; j++)
            byte8[j] = rand(); 

        a = clock();//write the time before reading array cycles
        for (int k = 0; k < n / 2; k++) /*divide n by the number of 
repetitions of the inner cycle, so that everywhere in
the same number of repetitions */
            for (int i = 0; i < 2; i++)
                l = byte8[i];
        cout << size(byte8) * 4 << "\tbytes\t" << clock() - a << endl; 
//output the number of ms needed for reading
// we repeat the same for arrays of longer length


        volatile int byte16[4];
        for (int j = 0; j < 4; j++)
            byte16[j] = rand();

        a = clock();
        for (int k = 0; k < n / 4; k++)
            for (int i = 0; i < 4; i++)
                l = byte16[i];
        cout << size(byte16) * 4 << "\tbytes\t" << clock() - a << endl;

        volatile int byte32[8];
        for (int j = 0; j < 8; j++)
            byte32[j] = rand();

        a = clock();
        for (int k = 0; k < n / 8; k++)
            for (int i = 0; i < 8; i++)
                l = byte32[i];
        cout << size(byte32) * 4 << "\tbytes\t" << clock() - a << endl;
        /*
        int byte60[15];
        for (int j = 0; j < 15; j++)
        byte60[j] = rand();

        a = clock();
        for (int k = 0; k < n / 15; k++)
        for (int i = 0; i < 15; i++)
        l = byte60[i];
        cout << size(byte60) * 4 << "\tbytes\t" << clock() - a << endl;
        */
        volatile int byte64[16];
        for (int j = 0; j < 16; j++)
            byte64[j] = rand();

        a = clock();
        for (int k = 0; k < n / 16; k++)
            for (int i = 0; i < 16; i++)
                l = byte64[i];
        cout << size(byte64) * 4 << "\tbytes\t" << clock() - a << endl;
        /*
        int byte68[17];
        for (int j = 0; j < 17; j++)
        byte68[j] = rand();

        a = clock();
        for (int k = 0; k < n / 17; k++)
        for (int i = 0; i < 17; i++)
        l = byte68[i];
        cout << size(byte68) * 4 << "\tbytes\t" << clock() - a << endl;
        */
        volatile int byte96[24];
        for (int j = 0; j < 24; j++)
            byte96[j] = rand();

        a = clock();
        for (int k = 0; k < n / 24; k++)
            for (int i = 0; i < 24; i++)
                l = byte96[i];
        cout << size(byte96) * 4 << "\tbytes\t" << clock() - a << endl;

        volatile int byte128[32];
        for (int j = 0; j < 32; j++)
            byte128[j] = rand();

        a = clock();
        for (int k = 0; k < n / 32; k++)
            for (int i = 0; i < 32; i++)
                l = byte128[i];
        cout << size(byte128) * 4 << "\tbytes\t" << clock() - a << endl;

        volatile int byte192[48];
        for (int j = 0; j < 48; j++)
            byte192[j] = rand();

        a = clock();
        for (int k = 0; k < n / 48; k++)
            for (int i = 0; i < 48; i++)
                l = byte192[i];
        cout << size(byte192) * 4 << "\tbytes\t" << clock() - a << endl;

        volatile int byte256[64];
        for (int j = 0; j < 64; j++)
            byte256[j] = rand();

        a = clock();
        for (int k = 0; k < n / 64; k++)
            for (int i = 0; i < 64; i++)
                l = byte256[i];
        cout << size(byte256) * 4 << "\tbytes\t" << clock() - a << endl;

        volatile int byte512[128];
        for (int j = 0; j < 128; j++)
            byte512[j] = rand();

        a = clock();
        for (int k = 0; k < n / 128; k++)
            for (int i = 0; i < 128; i++)
                l = byte512[i];
        cout << size(byte512) * 4 << "\tbytes\t" << clock() - a << endl;


        cout << "Repeat?(y/n): ";
        cin >> ask;
        cout << endl;
    }
    system("pause");
    return 0;
}
  • 1步,时间369
  • 2步,时间184
  • 3步,时间123
  • 4步,时间101
  • 5步,时间77
  • 6步,时间60
  • 7步,时间52
  • 8步,时间45
  • 9步,时间44
  • 10步,时间38
  • 11步,时间33
  • 12步,时间32
  • 13步,时间29
  • 14步,时间26
  • 15步,时间31
  • 16步,时间26
  • 17步,时间22
  • 18步,时间21
  • 19步,时间20
  • 20步,时间18
  • 21步,时间18
  • 22步,时间18
  • 23步,时间16
  • 24步,时间16
  • 25步,时间15
  • 26步,时间16
  • 27步,时间15
  • 28步,时间21
  • 29步,时间14
  • 30步,时间13
  • 31步,时间12
  • 32步,时间12
  • 33步,时间12
  • 34步,时间13
  • 35步,时间11
  • 36步,时间12
  • 37步,时间11
  • 38步,时间10
  • 39步,时间11
  • 40步,时间18
  • 41步,时间10
  • 42步,时间10
  • 43步,时间10
  • 44步,时间26
  • 45步,时间9
  • 46步,时间9
  • 47步,时间9
  • 48步,时间8
  • 49步,时间9
  • 50步,时间8
  • 51步,时间9
  • 52步,时间8
  • 53步,时间8
  • 54步,时间8
  • 55步,时间8
  • 56步,时间7
  • 57步,时间7
  • 58步,时间8
  • 59步,时间7
  • 60步,时间7
  • 61步,时间7
  • 62步,时间7
  • 63步,时间6
  • 64步,时间7
  • 重复?(y/n):n

  • 8 字节 1227

  • 16 字节 1736
  • 32 字节 951
  • 64 字节 862
  • 96 字节 805
  • 128 字节 805
  • 192 字节 769
  • 256 字节 1232
  • 512 字节 909
  • 重复?(是/否):是

  • 8 字节 1220

  • 16 字节 1739
  • 32 字节 944
  • 64 字节 842
  • 96 字节 815
  • 128 字节 804
  • 192 字节 781
  • 256 字节 1220
  • 512 字节 905
  • 重复?(是/否):

关键是我不能使用GetLogicalProcessorInformation之类的函数,我需要定义一个测试。我在 Visual Studio 2017 中启动,并在 Release 上进行了配置。对不起我的英语不好

4

0 回答 0