Created
December 31, 2013 15:36
-
-
Save fredrik-johansson/8198480 to your computer and use it in GitHub Desktop.
mpir/gmp exp(1) benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
#include "gmp.h" | |
#include "time.h" | |
#define ulong unsigned long | |
void | |
mpz_bsplit(mpz_t p, mpz_t q, ulong a, ulong b) | |
{ | |
if (b - a == 1) | |
{ | |
mpz_set_ui(p, 1); | |
mpz_set_ui(q, b); | |
} | |
else | |
{ | |
ulong m; | |
mpz_t p1, q2; | |
mpz_init(p1); | |
mpz_init(q2); | |
m = a + (b - a) / 2; | |
mpz_bsplit(p1, q, a, m); | |
mpz_bsplit(p, q2, m, b); | |
mpz_mul(p1, p1, q2); | |
mpz_add(p, p, p1); | |
mpz_mul(q, q, q2); | |
mpz_clear(p1); | |
mpz_clear(q2); | |
} | |
} | |
void mpz_e(mpz_t y, ulong prec) | |
{ | |
ulong N; | |
mpz_t p, q; | |
N = 1.1 * prec / log(prec) + 20; | |
mpz_init(p); | |
mpz_init(q); | |
mpz_bsplit(p, q, 0, N); | |
mpz_add(p, p, q); | |
mpz_mul_2exp(p, p, prec); | |
mpz_tdiv_q(y, p, q); | |
mpz_clear(p); | |
mpz_clear(q); | |
} | |
void time_e(void) | |
{ | |
ulong digits, prec, count, iter; | |
clock_t t; | |
mpz_t y; | |
mpz_init(y); | |
printf("\n"); | |
for (digits = 100; digits <= 10000000; digits *= 10) | |
{ | |
prec = digits * 3.333; | |
printf("digits = %lu: ", digits); | |
fflush(stdout); | |
iter = 1; | |
if (digits == 100) iter = 100000; | |
if (digits == 1000) iter = 10000; | |
if (digits == 10000) iter = 100; | |
if (digits == 100000) iter = 10; | |
if (digits == 1000000) iter = 1; | |
t = clock(); | |
for (count = 0; count < iter; count++) | |
mpz_e(y, prec); | |
t = clock() - t; | |
printf("%g s\n", ((double) t) / CLOCKS_PER_SEC / iter); | |
} | |
mpz_clear(y); | |
} | |
int main() | |
{ | |
time_e(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment