我想知道如何快速在文件中编写mpz_t
个GMP库。例如,我有以下C代码:
#include <stdio.h>
#include <gmp.h>
#include <time.h>
#include <stdlib.h>
int main(){
mpz_t p, q, inter1, inter2, tmp;
int i;
double t;
FILE* fichier;
mpz_inits(p, q, inter1, inter2, tmp, NULL);
mpz_set_ui(p, 1);
mpz_set_ui(q, 1);
t = (double) clock();
for (i = 0; i < 25; i++) {
mpz_set(tmp, p);
mpz_mul(inter1, q, q);
mpz_mul_ui(inter1, inter1, 2);
mpz_mul(inter2, p, p);
mpz_add(p, inter1, inter2);
mpz_mul(q, tmp, q);
mpz_mul_ui(q, q, 2);
}
printf("%lf\n", ((double) clock() - t)/CLOCKS_PER_SEC);
t = (double) clock();
fichier = fopen("num.txt", "w");
mpz_out_str(fichier, 10, p);
fclose(fichier);
printf("%lf\n", ((double) clock() - t)/CLOCKS_PER_SEC);
mpz_clears(p, q, inter1, inter2, tmp, NULL);
}
计算变量p
的值只需要6个secondes,而要在文件中写入此变量的值,需要22个secondes。
如何改善文件中的写作时间?