wangtongada / gmpy

Automatically exported from code.google.com/p/gmpy
GNU Lesser General Public License v3.0
0 stars 0 forks source link

Converting long -> mpz is slow #7

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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)

Original issue reported on code.google.com by fredrik....@gmail.com on 23 Jul 2008 at 9:18

GoogleCodeExporter commented 8 years ago
I 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!

Original comment by alea...@gmail.com on 23 Jul 2008 at 11:51

GoogleCodeExporter commented 8 years ago
I'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

Original comment by casevh on 30 Jul 2008 at 6:27

GoogleCodeExporter commented 8 years ago
Committed 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

Original comment by casevh on 1 Aug 2008 at 4:48