- 注册时间
- 2007-12-28
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 12785
- 在线时间
- 小时
|
楼主 |
发表于 2011-11-8 19:50:55
|
显示全部楼层
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <memory.h>
-
- #include "currTime.h"
-
- typedef unsigned long DWORD;
-
-
- void initAccessTable(int accessTab[], int len, int isRand )
- {
- int i,idx;
-
- if (isRand==0) //sequential access
- {
- for (i=0;i<len;i++)
- {
- accessTab[i]=i;
- }
- }
- else
- {
- char *tmpBuff=(char *)malloc(len);
- memset(tmpBuff,0,len);
-
- for (i=0;i<len;)
- {
- idx= ((rand() << 15)+ rand()) % len;
- if ( tmpBuff[idx]==0)
- {
- accessTab[i++]=idx;
- tmpBuff[idx]=1;
- }
- }
- free(tmpBuff);
- }
- }
-
- int sum(DWORD pBuff[], int accessTab[], int len)
- {
- DWORD s=0;
- int i,idx;
- for (i=0;i<len;i++)
- {
- idx=accessTab[i];
- s += pBuff[idx];
- }
- return s;
- }
-
- //mode=0, sequential access
- //mode=1, random access
- void test_access_RAM(int blk_len, int mode)
- {
- DWORD *pBuff=malloc(sizeof(DWORD)*blk_len);
- int *accessTab=malloc(sizeof(int)*blk_len);
- DWORD i,s;
- double t;
-
- for (i=0;i<blk_len;i++)
- {
- pBuff[i]=rand() & 15;
- }
-
- initAccessTable(accessTab, blk_len, mode);
-
- t=currTime();
- s=sum(pBuff,accessTab,blk_len);
- t=currTime()-t;
-
- printf("s=%d\n",s);
- if ( mode ==0)
- printf("sequential access %d number, it take %.6f ms\n",blk_len,t*1000 );
- else
- printf("random access %d number, it take %.6f ms\n",blk_len,t*1000 );
-
- free(pBuff);
- free(accessTab);
-
- }
-
- void test()
- {
- int blk_len;
- int i,loop;
-
- loop=3;
- blk_len=1024*1024;
-
- for (i=0;i<loop;i++)
- { test_access_RAM(blk_len,0); }
-
- for (i=0;i<loop;i++)
- { test_access_RAM(blk_len,1); }
-
- //----------------------------
- blk_len=4*1024*1024;
- for (i=0;i<loop;i++)
- { test_access_RAM(blk_len,0); }
-
- for (i=0;i<loop;i++)
- { test_access_RAM(blk_len,1); }
-
- //----------------------------
- blk_len=16*1024*1024;
- for (i=0;i<loop;i++)
- { test_access_RAM(blk_len,0); }
-
- for (i=0;i<loop;i++)
- { test_access_RAM(blk_len,1); }
-
- //----------------------------
- blk_len=64*1024*1024;
- for (i=0;i<loop;i++)
- { test_access_RAM(blk_len,0); }
-
- for (i=0;i<loop;i++)
- { test_access_RAM(blk_len,1); }
- }
-
- int main(int argc, char* argv[])
- {
- test();
- return 0;
- }
复制代码 |
|