小编Sim*_*ksy的帖子

Numpy `matmul` 在数组视图上的性能比 `dot` 差约 100 倍

我注意到matmulnumpy 中的函数的性能比乘以数组视图时的函数要差得多dot。在这种情况下,我的数组视图是复杂数组的真实部分。这是重现该问题的一些代码:

import numpy as np
from timeit import timeit
N = 1300
xx = np.random.randn(N, N) + 1j
yy = np.random.randn(N, N) + 1J

x = np.real(xx)
y = np.real(yy)
assert np.shares_memory(x, xx)
assert np.shares_memory(y, yy)

dot = timeit('np.dot(x,y)', number = 10, globals = globals())
matmul = timeit('np.matmul(x,y)', number = 10, globals = globals())

print('time for np.matmul: ', matmul)
print('time for np.dot: ', dot)
Run Code Online (Sandbox Code Playgroud)

在我的机器上输出如下:

time for np.matmul:  23.023062199994456
time for np.dot:  0.2706864000065252
Run Code Online (Sandbox Code Playgroud)

这显然与共享内存有关,因为替换np.real(xx) …

python performance numpy

5
推荐指数
1
解决办法
159
查看次数

标签 统计

numpy ×1

performance ×1

python ×1