Numpy中matmul对数组视图的运算性能比dot低约100倍
Numpy matmul 对复数数组实部视图的性能远低于 dot 的问题
我发现Numpy的matmul函数在处理复数数组的实部视图(与原数组共享内存的视图)时,性能表现远不如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)
本地运行后的输出结果:
time for np.matmul: 23.023062199994456 time for np.dot: 0.2706864000065252
将np.real(xx)替换为np.real(xx).copy()后,二者的性能差异完全消失,说明该问题的根源在于内存共享。但目前Numpy的官方文档并未提及此类场景的实现细节。
内容的提问来源于stack exchange,提问作者Simon Tartakovksy
相关产品推荐
相关产品推荐

