Why is gmpy2 so slow at complex exponentiation?

311 views Asked by At

I was using complex numbers in gmpy2 and noticed that it was slow. I narrowed it down the exponentiation operator. At first I thought it was just because it was complex. But then I compared it to mpmath which uses gmpy2 and it was so much faster:

# tested using gmpy2 2.0.8, mpmath 1.1.0, python 3.8.5
>>> import timeit
>>> setup = '''
import gmpy2 as gm
import mpmath

a1 = gm.mpc(-12.5, 34.125)
a2 = gm.mpc(17, -45.875)

b1 = mpmath.mpc(-12.5, 34.125)
b2 = mpmath.mpc(17, -45.875)
'''

# using gmpy2
>>> timeit.timeit('a1 ** a2', setup)
87.13848301399992
>>> timeit.timeit('a1 ** 2', setup)
40.478690218
>>> timeit.timeit('pow(a1, 2)', setup)
40.70392542999991

# using mpmath
>>> timeit.timeit('b1 ** b2', setup)
51.799312732999965
>>> timeit.timeit('b1 ** 2', setup)
4.239320562999978
>>> timeit.timeit('pow(b1, 2)', setup)
4.293315565000057

# multiplication comparison
>>> timeit.timeit('a1 * a1', setup)
0.9900801109999975  # gmpy2
>>> timeit.timeit('b1 * b1', setup)
4.711916033999955  # mpmath

Pure complex exponentiation is incredibly slow, but mpmath is still some 40% faster than gmpy2. Since mpmath is Python I figured it would be much slower but that's clearly not the case. How is gmpy2 so slow here?

1

There are 1 answers

2
casevh On BEST ANSWER

Disclaimer: I maintain gmpy2.

I was curious about the cause(s) of the differences. I ran four different tests.

# Reference test on Windows 10 that used the same gmpy2
# binaries.

>>> timeit.timeit('a1 ** a2', setup)
60.565931600000006
>>> timeit.timeit('a1 ** 2', setup)
25.686232700000005
>>> timeit.timeit('pow(a1, 2)', setup)
25.684606899999977
>>> timeit.timeit('b1 ** b2', setup)
35.29716189999999
>>> timeit.timeit('b1 ** 2', setup)
2.6226074000000494
>>> timeit.timeit('pow(b1, 2)', setup)
2.6126720999999975
>>>
>>> import gmpy2
>>> gmpy2.version()
'2.0.8'
>>> gmpy2.mp_version()
'MPIR 2.7.2'
>>> gmpy2.mpfr_version()
'MPFR 3.1.4'
>>> gmpy2.mpc_version()
'MPC 1.0.3'
>>>

The results are similar to those in the question. I printed the versions of the underlying libraries.

# Test using WSL with latest Ubuntu version. Same physical
# system as above.

>>> timeit.timeit('a1 ** a2', setup)
31.21574370000002
>>> timeit.timeit('a1 ** 2', setup)
2.3873958000000357
>>> timeit.timeit('pow(a1, 2)', setup)
2.3556844999999953
>>> timeit.timeit('b1 ** b2', setup)
36.35650579999998
>>> timeit.timeit('b1 ** 2', setup)
2.4482329999999592
>>> timeit.timeit('pow(b1, 2)', setup)
2.431874800000003
>>>
>>> import gmpy2
>>> gmpy2.version()
'2.1.0b3'
>>> gmpy2.mp_version()
'GMP 6.2.0'
>>> gmpy2.mpfr_version()
'MPFR 4.0.2'
>>> gmpy2.mpc_version()
'MPC 1.1.0'
>>>

I chose WSL because it is easy to install on Windows 10. gmpy2 and mpmath were installed using sudo apt install python3-gmpy2 and sudo apt install python3-mpmath. gmpy2 is slightly faster than mpmath.

# Test using Hyper-V virtual machine under Windows Server 2016.
# Different physical system but identical specifications.

>>> timeit.timeit('a1 ** a2', setup)
27.467059508984676
>>> timeit.timeit('a1 ** 2', setup)
2.171035467006732
>>> timeit.timeit('pow(a1, 2)', setup)
2.193065536994254
>>> timeit.timeit('b1 ** b2', setup)
31.870763173996238
>>> timeit.timeit('b1 ** 2', setup)
2.019194034015527
>>> timeit.timeit('pow(b1, 2)', setup)
2.0843256690131966
>>> 
>>> import gmpy2
>>> gmpy2.version()
'2.1.0b5'
>>> gmpy2.mp_version()
'GMP 6.2.0'
>>> gmpy2.mpfr_version()
'MPFR 4.0.2'
>>> gmpy2.mpc_version()
'MPC 1.1.0'
>>> 

I used the latest beta release for the previous test. The results are identical with the Ubuntu version. Overall, slightly faster that WSL.

# Same as above but using gmpy2 2.0.8 instead of 2.1.0b5.

>>> timeit.timeit('a1 ** a2', setup)
23.692542312986916
>>> timeit.timeit('a1 ** 2', setup)
9.208024947001832
>>> timeit.timeit('pow(a1, 2)', setup)
9.388882965984521
>>> timeit.timeit('b1 ** b2', setup)
32.078784318000544
>>> timeit.timeit('b1 ** 2', setup)
2.027712993003661
>>> timeit.timeit('pow(b1, 2)', setup)
2.123160599003313
>>> 
>>> import gmpy2
>>> gmpy2.version()
'2.0.8'
>>> gmpy2.mp_version()
'GMP 6.2.0'
>>> gmpy2.mpfr_version()
'MPFR 4.0.2'
>>> gmpy2.mpc_version()
'MPC 1.1.0'
>>>

The last two test show the difference between the 2.0.8 and 2.1.0 versions. I made significant changes to the argument handling. mpc ** int is much faster but mpc ** mpc is slightly slower. (I think I can fix that regression...)

The Windows binaries are using old versions of the underlying libraries. I am working towards Windows binaries based on the latest versions of GMP, MPFR, and MPC compiled with the mingw-w64 compilers. The GCC compiler will allow GMP to automatically select the proper code path for different CPUs.

Update 1

I've optimized mpc ** mpc and mpc ** int. The performance regression for mpc ** mpc has been fixed and mpc ** int is even faster.