- 注册时间
- 2007-12-28
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 12785
- 在线时间
- 小时
|
发表于 2016-5-2 16:27:37
|
显示全部楼层
这里给出我的测试程序。
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- #include "gmp.h"
- static LARGE_INTEGER freq;
- static BOOL initFreq()
- {
- BOOL ret;
- if ( !QueryPerformanceFrequency( &freq) )
- { ret=FALSE; }
- else
- { ret=TRUE; }
- return ret;
- }
- double currTime() //使用高精度计时器
- {
- LARGE_INTEGER performanceCount;
- BOOL result;
- double time=0.0;
- BOOL bRet=TRUE;
- if (freq.QuadPart==0)
- {
- bRet=initFreq();
- }
-
- if (bRet)
- {
- result=QueryPerformanceCounter( &performanceCount );
- time= performanceCount.HighPart * 4294967296.0 + performanceCount.LowPart;
- time=time / ( freq.HighPart * 4294967296.0 + freq.LowPart);
- }
- return time;
- }
- void generate_an_integer(int dig_len, mpz_t x)
- {
- int i;
- char c;
- char *pBuff=NULL;
- pBuff=(char *) malloc(dig_len+8);
- if ( pBuff==NULL)
- {
- printf("Alloc memory failed on %s:%d\n",__FILE__,__LINE__);
- return;
- }
-
- srand(GetTickCount());
- while (1)
- {
- c=rand() % 10;
- if (c >0)
- {
- pBuff[0]= c + 0x30;
- break;
- }
- }
-
- for (i=1;i<dig_len;i++)
- pBuff[i]=(rand() % 10)+0x30;
- pBuff[i]=0;
-
- mpz_set_str (x,pBuff,10);
- if ( pBuff!=NULL)
- {
- free(pBuff); pBuff=NULL;
- }
- }
- void test_mpz_mul(int dig_len)
- {
- int j,test_count;
-
- mpz_t a;
- mpz_t b;
- mpz_t prod;
- mpz_t s;
- int len;
- size_t r_len;
- double startTime;
- double testTime;
- //---------------------
-
- test_count=1000;
- r_len = dig_len *10/3+32;
- mpz_init(a);
- mpz_init(b);
- mpz_init2(prod,r_len);
- mpz_init2(s,r_len);
- generate_an_integer(dig_len,a);
- generate_an_integer(dig_len,b);
- mpz_set_ui(s,0);
- //--------------------------------------
- startTime=currTime();
- for (j=0;j<test_count;j++)
- {
- mpz_mul (prod,a,b);
- mpz_add(s,s,prod);
- }
- testTime=currTime()-startTime;
- testTime = testTime * 1000 / (test_count); //get unit time by ms
-
- len=mpz_sizeinbase(s,10);
- printf("the result contain about %d digital\n", len);
- printf("It take %.4lf ms\n", testTime);
-
- mpz_clear(a);
- mpz_clear(b);
- mpz_clear(prod);
- mpz_clear(s);
- }
复制代码 |
|