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.