Converting long -> mpz is extremely slow. Try for example:
from gmpy import mpz a = 10**500000 b = mpz(a)
A workaround is to use:
c = mpz('%x'%a, 16)
Comment #1
Posted on Jul 23, 2008 by Quick DogI can reproduce the problem; e.g.: $ python -mtimeit -s'from gmpy import mpz; a=10**5000' 'x=mpz(a)' 1000 loops, best of 3: 1.14 msec per loop $ python -mtimeit -s'from gmpy import mpz; a=10**5000' 'x=mpz("%x"%a, 16)' 10000 loops, best of 3: 107 usec per loop
$ python -mtimeit -s'from gmpy import mpz; a=10**500' 'x=mpz(a)' 10000 loops, best of 3: 21.2 usec per loop $ python -mtimeit -s'from gmpy import mpz; a=10**500' 'x=mpz("%x"%a, 16)' 100000 loops, best of 3: 16.2 usec per loop
So the direct construction from long appears to be degrading worse-than-O(N). Haven't had time to examine the sources to find out why that is so, but look forward to doing so -- thaks!
Comment #2
Posted on Jul 30, 2008 by Grumpy WombatI've fixed long2mpz by using the GMP function mpz_import. It is much faster. I'm still working updating mpz2long to use mpz_export. I'll try to get a patch in the next couple of days.
casevh
Comment #3
Posted on Aug 1, 2008 by Grumpy WombatCommitted r37 to improve performance.
Before: case@hp2:~/svn/gmpy$ py25 -mtimeit -s'from gmpy import mpz; a=10**500' 'x=mpz(a)' 100000 loops, best of 3: 7.25 usec per loop case@hp2:~/svn/gmpy$ py25 -mtimeit -s'from gmpy import mpz; a=10**5000' 'x=mpz(a)' 1000 loops, best of 3: 270 usec per loop case@hp2:~/svn/gmpy$ py25 -mtimeit -s'from gmpy import mpz; a=10**50000' 'x=mpz(a)' 10 loops, best of 3: 22.7 msec per loop
After: case@hp2:~/svn/gmpy$ py25 -mtimeit -s'from gmpy import mpz; a=10**500' 'x=mpz(a)' 1000000 loops, best of 3: 1.05 usec per loop case@hp2:~/svn/gmpy$ py25 -mtimeit -s'from gmpy import mpz; a=10**5000' 'x=mpz(a)' 100000 loops, best of 3: 8.67 usec per loop case@hp2:~/svn/gmpy$ py25 -mtimeit -s'from gmpy import mpz; a=10**50000' 'x=mpz(a)' 10000 loops, best of 3: 85.2 usec per loop
Thanks for report.
casevh
Status: Fixed
Labels:
Type-Defect
Priority-High