1
我想要測量的高速緩存的速度(I問question有關),所以我寫這樣的代碼:遍歷的訪問時間經由恆定長度的陣列
#include<cstdio>
#include<ctime>
#include<windows.h>
const int mod = 100000007;
volatile int f[100000007];
volatile int *p;
int main()
{
FILE *f1;
f1=fopen("CacheTest.txt","w");
for(int l=1;l<=100000;)
{
clock_t st,ed;
st=clock();
int now=0;
for(int i=0;i<mod;i++)
{
now+=l;//note:mod is a prime and that will traversal the array
if (now>=mod) now-=mod;
}
ed=clock();
double extime=(double)(ed-st)/CLOCKS_PER_SEC;
st=clock();
now=0;
for(int i=0;i<mod;i++)
{
p=&f[now];
*p=1;
now+=l;
if (now>=mod) now-=mod;
}
ed=clock();
double cost=(double)(ed-st)/CLOCKS_PER_SEC;
fprintf(f1,"length %d costs %f seconds access time:%f\n",l,cost,cost-extime);
printf("length %d costs %f seconds access time:%f\n",l,cost,cost-extime);
if (l<50) l++;
else if (l<100) l+=2;
else if (l<1000) l+=20;
else if (l<10000) l+=200;
else if (l<100000) l+=2000;
else l+=20000;
}
}
結果如下:
length 1 costs 0.994000 seconds access time:0.153000
length 2 costs 0.651000 seconds access time:-0.048000
length 3 costs 1.034000 seconds access time:0.414000
length 4 costs 1.151000 seconds access time:0.018000
length 5 costs 1.221000 seconds access time:0.086000
length 6 costs 1.313000 seconds access time:0.179000
length 7 costs 1.475000 seconds access time:0.442000
length 8 costs 1.584000 seconds access time:0.437000
length 9 costs 1.700000 seconds access time:0.567000
length 10 costs 1.672000 seconds access time:0.665000
length 11 costs 1.578000 seconds access time:0.573000
length 12 costs 2.166000 seconds access time:1.280000
length 13 costs 2.070000 seconds access time:0.988000
length 14 costs 2.430000 seconds access time:1.402000
length 15 costs 2.564000 seconds access time:1.428000
length 16 costs 2.651000 seconds access time:1.519000
length 17 costs 2.721000 seconds access time:1.654000
length 18 costs 2.648000 seconds access time:1.518000
length 19 costs 2.775000 seconds access time:1.638000
length 20 costs 2.757000 seconds access time:1.636000
length 21 costs 2.816000 seconds access time:1.684000
length 22 costs 2.842000 seconds access time:1.706000
length 23 costs 2.856000 seconds access time:1.751000
length 24 costs 2.702000 seconds access time:1.568000
length 25 costs 2.624000 seconds access time:1.618000
length 26 costs 2.912000 seconds access time:1.777000
length 27 costs 2.617000 seconds access time:1.468000
length 28 costs 2.910000 seconds access time:1.778000
length 29 costs 2.779000 seconds access time:1.649000
length 30 costs 2.803000 seconds access time:1.727000
length 31 costs 2.596000 seconds access time:1.465000
length 32 costs 2.068000 seconds access time:1.188000
length 33 costs 2.012000 seconds access time:1.294000
length 34 costs 3.258000 seconds access time:2.381000
length 35 costs 3.538000 seconds access time:2.406000
length 36 costs 3.619000 seconds access time:2.556000
length 37 costs 3.847000 seconds access time:2.717000
length 38 costs 3.958000 seconds access time:2.827000
length 39 costs 3.910000 seconds access time:2.841000
length 40 costs 3.722000 seconds access time:2.592000
length 41 costs 4.095000 seconds access time:2.960000
length 42 costs 3.440000 seconds access time:2.420000
length 43 costs 4.217000 seconds access time:3.085000
length 44 costs 4.413000 seconds access time:3.457000
length 45 costs 4.637000 seconds access time:3.507000
length 46 costs 4.248000 seconds access time:3.115000
length 47 costs 4.924000 seconds access time:3.856000
length 48 costs 5.072000 seconds access time:3.942000
length 49 costs 4.619000 seconds access time:3.615000
length 50 costs 5.025000 seconds access time:4.136000
length 52 costs 5.196000 seconds access time:4.059000
length 54 costs 4.937000 seconds access time:3.806000
length 56 costs 5.264000 seconds access time:4.132000
length 58 costs 5.137000 seconds access time:4.004000
length 60 costs 5.113000 seconds access time:4.107000
length 62 costs 4.665000 seconds access time:3.537000
length 64 costs 5.030000 seconds access time:3.900000
length 66 costs 4.969000 seconds access time:4.016000
length 68 costs 5.201000 seconds access time:4.096000
length 70 costs 5.066000 seconds access time:3.931000
...
for the length 70~10000 , the access time is around 4s.
前幾行的訪問時間很低,我不知道爲什麼。
然後程序運行穩定,訪問時間大約爲0.4s,我認爲原因是大多數訪問都碰到了L1級緩存。
隨着更大的長度,時間變成1.5秒左右。也許一級緩存不起作用,但二級緩存起作用。
然後時間到了4秒,也許根本沒有緩存命中?
上述結果似乎符合我的預期。
但是!意外的事情發生了,我感到驚訝。
當超過20000長度,訪問時間變爲:
length 20000 costs 1.828000 seconds access time:1.453000
length 22000 costs 1.867000 seconds access time:1.492000
length 24000 costs 1.959000 seconds access time:1.584000
length 26000 costs 1.977000 seconds access time:1.603000
length 28000 costs 1.987000 seconds access time:1.613000
length 30000 costs 2.045000 seconds access time:1.669000
length 32000 costs 1.951000 seconds access time:1.575000
length 34000 costs 1.980000 seconds access time:1.611000
length 36000 costs 1.988000 seconds access time:1.613000
length 38000 costs 1.993000 seconds access time:1.619000
length 40000 costs 1.850000 seconds access time:1.473000
length 42000 costs 2.010000 seconds access time:1.634000
length 44000 costs 1.991000 seconds access time:1.615000
length 46000 costs 1.977000 seconds access time:1.601000
length 48000 costs 1.977000 seconds access time:1.601000
length 50000 costs 1.927000 seconds access time:1.552000
length 52000 costs 1.907000 seconds access time:1.531000
length 54000 costs 2.013000 seconds access time:1.637000
length 56000 costs 2.014000 seconds access time:1.639000
length 58000 costs 2.011000 seconds access time:1.640000
length 60000 costs 1.978000 seconds access time:1.606000
length 62000 costs 2.046000 seconds access time:1.672000
length 64000 costs 2.076000 seconds access time:1.700000
length 66000 costs 2.068000 seconds access time:1.693000
length 68000 costs 2.014000 seconds access time:1.639000
length 70000 costs 2.123000 seconds access time:1.748000
length 72000 costs 2.032000 seconds access time:1.646000
length 74000 costs 2.015000 seconds access time:1.638000
length 76000 costs 2.023000 seconds access time:1.646000
length 78000 costs 1.873000 seconds access time:1.497000
length 80000 costs 1.655000 seconds access time:1.278000
任何人都可以解釋爲什麼這些奇怪的事情發生?我現在卡住了,不知道這件事。
在此先感謝。
我無法解釋,但我會分享我的系統的數據。我把質數減少到10000019,因爲我沒有你那麼多的記憶。對於較小的長度,它會逐漸增長到1.6個訪問時間(長度爲8),然後保持穩定直到長度爲2400,然後增長到2.1個訪問時間(長度爲4200)並保持穩定,除了長度爲4800,6400, 9600,16000,20000,32000,40000,50000,64000,80000,100000。這是你所期望的,我想。你確定你的系統沒有被使用嗎? – 2013-04-27 08:46:23
你是對的,我再次測試我的代碼,現在這些事情不再發生。 – Sayakiss 2013-04-27 14:53:17
只是好奇,你有什麼樣的CPU /級別的緩存? – Michael 2013-05-13 03:49:36