- 注册时间
- 2007-12-28
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 12787
- 在线时间
- 小时
|
发表于 2012-2-13 13:38:33
|
显示全部楼层
这里贴出所有的代码,注意,需要修改 CPU_FREQ的定义,使得它的值和你的CPU一致。
编译环境为VC6.0-
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <windows.h>
-
- #define PI 3.1415926535897932384626433832795
- #define DATA_COUNT 1000
- #define CPU_FREQ (3.16e9) //CPU frequency, need always change this definition for match your CPU
-
- typedef double ( *lpfn_math )(double);
-
- unsigned __int64 s_u64Frequency = 1;
- unsigned __int64 s_u64Start, s_u64End;
-
- double data[DATA_COUNT];
- double result[DATA_COUNT];
-
- void testFun( const lpfn_math pFun,
- const char* lpszFunName,
- const int testTimes )
- {
-
- int i,j;
- double t;
- double ns;
-
- QueryPerformanceFrequency((LARGE_INTEGER *)&s_u64Frequency );
-
- printf( "\nTest function: %s() %u times\n",
- lpszFunName, testTimes*DATA_COUNT );
-
- QueryPerformanceCounter((LARGE_INTEGER *)&s_u64Start );
- for (i=0;i<testTimes;i++)
- {
- for (j=0;j<DATA_COUNT;j++)
- {
- result[j]=pFun(data[j]);
- }
- }
- QueryPerformanceCounter((LARGE_INTEGER *)&s_u64End );
-
- t = (double)(( s_u64End - s_u64Start ) / (double) s_u64Frequency );
- printf( "Elapsed time: %d ms\n", (int)(t*1000));
-
- ns=(t * 1e9) /(double)(DATA_COUNT *testTimes); //calc the time of one sqrt function call in nanosecond
- printf("%s() spend %f ns\n", lpszFunName,ns );
- printf("%s() spend %f cpu cycle\n", lpszFunName, (ns*1e9)/CPU_FREQ);
- }
-
- void test_sqrt()
- {
- int c,n1,n2;
- c=0;
- while (c<DATA_COUNT)
- {
- n1=rand();
- n2=rand();
- if (n1!=0 && n2!=0)
- {
- data[c]=double(n1) / double(n2);
- c++;
- }
- }
- testFun( sqrt,"sqrt",1000);
- }
-
- void test_sine()
- {
- int c,n;
- c=0;
- while (c<DATA_COUNT)
- {
- n=(rand() % 1000)+1;
- data[c]=double(n) / 1000.00 * PI * 0.5; //data[c] is i/1000*PI/2, i=1..1000
- c++;
- }
- testFun(sin,"sin",1000);
- }
-
- void test_log()
- {
- int c,n1,n2;
- c=0;
- while (c<DATA_COUNT)
- {
- n1=(rand() % 1000)+1;
- n2=rand() % 1000;
- data[c]=double(n1) + double(n2) * 0.001; //data[ i ] always between 1.000 to 1000.999
- c++;
- }
- testFun( log,"log",1000);
- }
-
- int main(int argc, char* argv[])
- {
- test_sqrt();
- test_sine();
- test_log();
- return 0;
- }
复制代码 |
|