C 不同整数大小的GMP定时差;
谁能告诉我,如果我使用不同大小的操作数,GMP所用的时间是如何不同的。 例如:下面的代码C 不同整数大小的GMP定时差;,c,time,multiplication,gmp,C,Time,Multiplication,Gmp,谁能告诉我,如果我使用不同大小的操作数,GMP所用的时间是如何不同的。 例如:下面的代码 #include <stdio.h> #include <gmp.h> #include <stdlib.h> #include <time.h> #define REPEAT 10000 void full_mult(mpz_t r,mpz_t a,mpz_t b) { mpz_t temp; mpz_init(temp); mp
#include <stdio.h>
#include <gmp.h>
#include <stdlib.h>
#include <time.h>
#define REPEAT 10000
void full_mult(mpz_t r,mpz_t a,mpz_t b)
{
mpz_t temp;
mpz_init(temp);
mpz_mul(r,a,b);
mpz_add(temp,a,b);
mpz_sub(a,a,b);
mpz_mul(temp,temp,a);
/*the above code 10 more times*/
}
void half_mult(mpz_t r,mpz_t a,mpz_t b)
{
mpz_t temp;
mpz_init(temp);
mpz_mul(r,a,b);
mpz_add(temp,a,b);
mpz_sub(a,a,b);
mpz_mul(temp,temp,a);
/*the above code then more times*/
}
void main()
{
long int i;
clock_t start, end;
double cpu_time_used;
gmp_randstate_t state;
gmp_randinit_mt(state);
mpz_t a[REPEAT];
mpz_t b[REPEAT];
mpz_t a1[REPEAT];
mpz_t b1[REPEAT];
mpz_t r[REPEAT];
mpz_t r1[REPEAT];
for(i=0;i<REPEAT;i++)
{
mpz_init(a[i]);mpz_init(b[i]);
mpz_init(a1[i]);mpz_init(b1[i]);
mpz_init(r[i]);mpz_init(r1[i]);
}
for(i=0;i<REPEAT;i++)
{
mpz_urandomb(a[i],state,128);
mpz_urandomb(b[i],state,128);
}
start=clock();
for(i=0;i<REPEAT;i++)
half_mult(r[i],a[i],b[i]);
end=clock();
printf( "Number of seconds: %f\n", (end-start)/(double)CLOCKS_PER_SEC );
for(i=0;i<REPEAT;i++)
{
mpz_urandomb(a1[i],state,256);
mpz_urandomb(b1[i],state,256);
}
start=clock();
for(i=0;i<REPEAT;i++)
full_mult(r1[i],a1[i],b1[i]);
end=clock();
printf( "Number of seconds: %f\n", (end-start)/(double)CLOCKS_PER_SEC );
}
#包括
#包括
#包括
#包括
#定义重复10000次
完全无效(mpz_t r、mpz_t a、mpz_t b)
{
mpz_t温度;
mpz_初始(温度);
mpz_mul(r,a,b);
mpz_添加(温度、a、b);
mpz_sub(a、a、b);
mpz_mul(临时,临时,a);
/*以上代码重复10次*/
}
无效一半(mpz_t r、mpz_t a、mpz_t b)
{
mpz_t温度;
mpz_初始(温度);
mpz_mul(r,a,b);
mpz_添加(温度、a、b);
mpz_sub(a、a、b);
mpz_mul(临时,临时,a);
/*上面的代码会重复多次*/
}
void main()
{
long int i;
时钟开始、结束;
使用双cpu时间;
gmp_randstate_t state;
gmp_randinit_mt(州);
mpz_t a[重复];
mpz_t b[重复];
mpz_t a1[重复];
mpz_t b1[重复];
mpz_t r[重复];
mpz_t r1[重复];
对于(i=0;i根据15.1节,库对不同大小的操作数使用不同的乘法算法。请参阅阈值表:
| Algorithm | Threshold |
|-----------|----------------------|
| Basecase | (none) |
| Karatsuba | MUL_TOOM22_THRESHOLD |
| Toom-3 | MUL_TOOM33_THRESHOLD |
| Toom-4 | MUL_TOOM44_THRESHOLD |
| Toom-6.5 | MUL_TOOM6H_THRESHOLD |
| Toom-8.5 | MUL_TOOM8H_THRESHOLD |
| FFT | MUL_FFT_THRESHOLD |
因此,由于算法不同,计时也可能不同。请尝试更长的样本。我已将“重复”更改为10000000和那些
mpz_t a[REPEAT];
mpz_t b[REPEAT];
mpz_t a1[REPEAT];
mpz_t b1[REPEAT];
mpz_t r[REPEAT];
mpz_t r1[REPEAT];
到
因此,跑了3次后,我得到:
$ gcc -O2 gmp_bench.c -lgmp
$ time ./a.out
Number of seconds: 12.689352
Number of seconds: 18.295134
./a.out 34.54s user 1.27s system 99% cpu 35.820 total
$ time ./a.out
Number of seconds: 12.647052
Number of seconds: 17.918326
./a.out 34.08s user 1.35s system 99% cpu 35.426 total
$ time ./a.out
Number of seconds: 12.647854
Number of seconds: 18.106714
./a.out 34.29s user 1.28s system 99% cpu 35.581 total
$
通过监测执行情况,我注意到分配的内存在不断增加,因此分配开销可能比算法本身更高。请使用宏的所有大写名称。并正确格式化和缩进代码。更改了宏。请告诉我缩进和格式化方面的问题。我将我很乐意改变。看看你的代码!如果仍然不确定“C缩进风格”。选择一个(如果可能的话,不要太奇特)并一致使用。每个mpz_init应该有一个相应的mpz_free。或者更好的是,为了测量乘法时间,应该重复使用相同的变量。
$ gcc -O2 gmp_bench.c -lgmp
$ time ./a.out
Number of seconds: 12.689352
Number of seconds: 18.295134
./a.out 34.54s user 1.27s system 99% cpu 35.820 total
$ time ./a.out
Number of seconds: 12.647052
Number of seconds: 17.918326
./a.out 34.08s user 1.35s system 99% cpu 35.426 total
$ time ./a.out
Number of seconds: 12.647854
Number of seconds: 18.106714
./a.out 34.29s user 1.28s system 99% cpu 35.581 total
$