【问题标题】:Why is gmpy2 so slow at complex exponentiation?为什么 gmpy2 在复幂运算中这么慢?
【发布时间】:2021-01-05 13:08:20
【问题描述】:

我在 gmpy2 中使用复数并注意到它很慢。我缩小了幂运算符的范围。起初我认为这只是因为它很复杂。但后来我将它与 使用 gmpy2 的 mpmath 进行了比较,它的速度要快得多:

# 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

纯复数幂运算非常慢,但 mpmath 仍然比 gmpy2 快 40%。由于 mpmath 是 Python,我认为它会慢得多,但显然情况并非如此。 gmpy2怎么这么慢?

【问题讨论】:

  • 您确定mpmath 使用的是gmpy2?文档说mpmath'将使用gmpy(如果有),而没有提及gmpy2(可能只是一个疏忽,一个项目是从另一个项目中产生的)。我不知道他们为跟上 gmpy/gmpy2 版本付出了多少努力,而且他们可能无法与 gmpy2 一起使用,因为它可能在 2.0 版本中发生了一些重大变化。
  • 能否请您检查并粘贴 python、gmpy2、、mpmath 的版本。还请检查所有 C 库,gmpy2 depends 是否已正确编译和安装。对于两个库来说,单次操作的结果非常很慢,我想问题在于 C 库在后台运行。
  • 抱歉,我没有注意到版本已经存在。我仍然认为你应该检查例如如果 libmpfrlibmpfr-dev(假设它的 debian 或类似的,或适用于您的操作系统的等效)已正确安装并正常工作
  • @ShadowRanger mpmath.libmp.BACKENDgmpy 即使 gmpy 没有安装 - mpmath 也将 try to import gmpy2 首先。
  • mpmath 将尝试使用gmpy2gmpy 来替换Python 的内置整数类型。 mpmath 不会调用gmpy2/gmpy 中的mpfrmpc 相关函数。稍后我会详细介绍。

标签: python exponentiation mpmath gmpy mpc


【解决方案1】:

免责声明:我维护gmpy2

我很好奇这些差异的原因。我进行了四个不同的测试。

# 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'
>>>

结果与问题中的结果相似。我打印了底层库的版本。

# 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'
>>>

我选择 WSL 是因为它很容易在 Windows 10 上安装。gmpy2mpmath 是使用 sudo apt install python3-gmpy2sudo apt install python3-mpmath 安装的。 gmpy2mpmath 稍快。

# 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'
>>> 

我在之前的测试中使用了最新的 beta 版本。结果与 Ubuntu 版本相同。总体而言,比 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'
>>>

最后两个测试显示了2.0.82.1.0 版本之间的差异。我对参数处理进行了重大更改。 mpc ** int 快得多,但 mpc ** mpc 稍慢。 (我想我可以修复那个回归...)

Windows 二进制文件使用的是旧版本的底层库。我正在开发基于使用 mingw-w64 编译器编译的最新版本的 GMP、MPFR 和 MPC 的 Windows 二进制文件。 GCC 编译器将允许 GMP 为不同的 CPU 自动选择正确的代码路径。

更新 1

我优化了mpc ** mpcmpc ** intmpc ** mpc 的性能回归已修复,mpc ** int 的速度更快。

【讨论】:

  • 所以最终,它很慢,因为它已经过时了。 2.1.0 版本什么时候出?无关:Windows 版本(至少来自 here 的编译轮)在 .conjugate 上调用 mpc 时也会导致 Python 崩溃。
  • 我设法获得了一个正在运行的二进制文件。它大约快 25%,但 WSL/Linux 版本更快。我还没有做任何详细的测试,所以它还没有准备好发布,但我越来越接近了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-01
  • 1970-01-01
  • 2012-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多