Python and GMP.

casevh casevh at gmail.com
Mon Apr 20 23:09:59 EDT 2009


On Apr 20, 11:39 am, Benjamin Peterson <benja... at python.org> wrote:
>  <alessiogiovanni.baroni <at> gmail.com> writes:
>
>
>
> > There are reasons why Python not used the GMP library for implementing
> > its long type?
>
> Basically, GMP only becomes faster when the numbers are huge.

Python 3.1 is significantly faster than Python 2.x on 64-bit
platforms. The following times are for multiplication with 2, 30 and
300 decimal digits.

Testing 2 digits. This primarily measures the overhead for call GMP
via an extension module.

$ py25 -m timeit -s  "a=long('23'*1);b=long('47'*1)" "c=a*b"
10000000 loops, best of 3: 0.15 usec per loop
$ py25 -m timeit -s  "a=int('23'*1);b=int('47'*1)" "c=a*b"
10000000 loops, best of 3: 0.0735 usec per loop
$ py31 -m timeit -s  "a=int('23'*1);b=int('47'*1)" "c=a*b"
10000000 loops, best of 3: 0.074 usec per loop
$ py25 -m timeit -s  "import gmpy;a=gmpy.mpz('23'*1);b=gmpy.mpz
('47'*1)" "c=a*b"
10000000 loops, best of 3: 0.121 usec per loop


Testing 30 digits. No significant increase in time with GMP.

$ py25 -m timeit -s  "a=long('23'*15);b=long('47'*15)" "c=a*b"
1000000 loops, best of 3: 0.343 usec per loop
$ py31 -m timeit -s  "a=int('23'*15);b=int('47'*15)" "c=a*b"
10000000 loops, best of 3: 0.142 usec per loop
$ py25 -m timeit -s  "import gmpy;a=gmpy.mpz('23'*15);b=gmpy.mpz
('47'*15)" "c=a*b"
10000000 loops, best of 3: 0.125 usec per loop

Testing 300 digits.

$ py25 -m timeit -s  "a=long('23'*150);b=long('47'*150)" "c=a*b"
100000 loops, best of 3: 12.5 usec per loop
$ py31 -m timeit -s  "a=int('23'*150);b=int('47'*150)" "c=a*b"
100000 loops, best of 3: 3.13 usec per loop
$ py25 -m timeit -s  "import gmpy;a=gmpy.mpz('23'*150);b=gmpy.mpz
('47'*150)" "c=a*b"
1000000 loops, best of 3: 0.673 usec per loop

Platform is 64-bit Linux with Core2 Duo processor. gmpy was linked
against MPIR 1.1. MPIR is an LGPLv2 fork of GMP and is significantly
faster than GMP 4.2.x.  The newly released GMP 4.3.0 is about 10%
faster yet.

casevh



More information about the Python-list mailing list