2

我想知道如何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);
}

计算变量的值只需 6 秒,p而将变量的值写入文件则需要 22 秒。

如何提高文件中的写入时间?

4

0 回答 0