我已经在 MATLAB 和 Python 中设置了两个相同的关于矩阵乘法和广播的测试。对于 Python,我使用了 NumPy,对于 MATLAB,我使用了使用 BLAS的mtimesx库。MATLABclose all; clear;N = 1000 + 100; % a few initial runs to be trimmed off at the enda = 100;b = 30;c = 40;d = 50;A = rand(b, c, a);B = rand(c, d, a);C = zeros(b, d, a);times = zeros(1, N);for ii = 1:N tic C = mtimesx(A,B); times(ii) = toc;endtimes = times(101:end) * 1e3;plot(times);grid on;title(median(times));Pythonimport timeitimport numpy as npimport matplotlib.pyplot as pltN = 1000 + 100 # a few initial runs to be trimmed off at the enda = 100b = 30c = 40d = 50A = np.arange(a * b * c).reshape([a, b, c])B = np.arange(a * c * d).reshape([a, c, d])C = np.empty(a * b * d).reshape([a, b, d])times = np.empty(N)for i in range(N): start = timeit.default_timer() C = A @ B times[i] = timeit.default_timer() - starttimes = times[101:] * 1e3plt.plot(times, linewidth=0.5)plt.grid()plt.title(np.median(times))plt.show()我的 Python 是pip使用 OpenBLAS安装的默认 Python 。我在英特尔 NUC i3 上运行。MATLAB 代码运行时间为 1 毫秒,而 Python 运行时间为 5.8 毫秒,我不知道为什么,因为它们似乎都在使用 BLAS。
添加回答
举报
0/150
提交
取消