/* testmul_gmp.cc Written by Chen Li , 10/00 $Id$ */ #include #include #include #include #include #include #include #include #include /* All GMP programs need to include gmp.h */ main (int argc, char **argv) { int L, n; if (argc < 3) { cerr << "Usage: qmul " << endl; exit(1); } L = atoi(argv[1]); n = atoi(argv[2]); unsigned int seed = (unsigned int) (time(NULL) % UINT_MAX); srand(seed); // srand(1234567); mpz_t x, y, r; /* Initialize variables */ mpz_init (x); mpz_init (y); mpz_init (r); gmp_randstate_t state; gmp_randinit (state, GMP_RAND_ALG_DEFAULT); long gmp_total_time = 0; struct tms *tbuf = new tms(); long u1, u2; for (int k = 0; k < n; k++) { /* Assign a and b from base 10 strings in argv */ mpz_urandomb (x, state, L); // x = bigRand(L); mpz_urandomb (y, state, L); // y = bigRand(L); // cout << "x: " << x << endl; // cout << "y: " << y << endl; times(tbuf); u1 = tbuf->tms_utime; /* Multiply a and b and put the result in p */ mpz_mul (r, x, y); times(tbuf); u2 = tbuf->tms_utime; gmp_total_time += (u2 - u1); cout << "GMP: u1 = " << u1 << ", u2 = " << u2 << ", time used " << (u2 - u1) << endl; #ifdef DEBUG /* Print p in base 10 */ mpz_out_str (stdout, 10, r); fputc ('\n', stdout); #endif } cout << "GMP Total time: " << (double)gmp_total_time / 100 << ", average time : " << (double)gmp_total_time / (100 * n) << endl; exit(0); /* Clear out variables */ mpz_clear (x); mpz_clear (y); mpz_clear (r); exit (0); }