MATLAB的速度是Numpy的两倍

MATLAB的速度是Numpy的两倍

本文介绍了MATLAB的速度是Numpy的两倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名工程研究生,目前出于数值模拟的目的,正在从MATLAB过渡到Python.我的印象是,对于基本的数组操作,Numpy的速度与MATLAB一样快.但是,对于我编写的两个不同程序,MATLAB的运行速度似乎是Numpy的两倍.我用于Numpy(Python 3.3)的测试代码是:

I am an engineering grad student currently making the transition from MATLAB to Python for the purposes of numerical simulation. I was under the impression that for basic array manipulation, Numpy would be as fast as MATLAB. However, it appears for two different programs I write that MATLAB is a little under twice as fast as Numpy. The test code I am using for Numpy (Python 3.3) is:

import numpy as np
import time

a = np.random.rand(5000,5000,3)

tic = time.time()
a[:,:,0] = a[:,:,1]
a[:,:,2] = a[:,:,0]
a[:,:,1] = a[:,:,2]
toc = time.time() - tic
print(toc)

我在使用MATLAB 2012a的地方:

Whereas for MATLAB 2012a I am using:

a = rand(5000,5000,3);

tic;
a(:,:,1) = a(:,:,2);
a(:,:,3) = a(:,:,1);
a(:,:,2) = a(:,:,3);
toc

我使用的算法是在NASA 网站上进行比较的算法. MATLAB.该网站显示,该算法的速度方面,Numpy超过了MATLAB.但是我的结果显示,Numpy的仿真时间为0.49 s,MATLAB的仿真时间为0.29 s.我还在Numpy和Matlab上都运行了高斯-塞德尔求解器,并且得到了相似的结果(16.5 s和9.5 s)

The algorithm I am using is the one used on a NASA website comparing Numpy and MATLAB. The website shows that Numpy surpasses MATLAB in terms of speed for this algorithm. Yet my results show a 0.49 s simulation time for Numpy and a 0.29 s simulation time for MATLAB. I also have run a Gauss-Seidel solver on both Numpy and Matlab and I get similar results (16.5 s vs. 9.5 s)

我是Python的新手,在编程方面也不是非常识字.我使用的是WinPython 64位Python发行版,但也尝试使用Pythonxy无济于事.

I am brand new to Python and am not extremely literate in terms of programming. I am using the WinPython 64 bit Python distribution but have also tried Pythonxy to no avail.

我读过的应该提高性能的一件事是使用MKL构建Numpy.不幸的是,我不知道如何在Windows上执行此操作.我什至需要这样做吗?

One thing I have read which should improve performance is building Numpy using MKL. Unfortunately I have no idea how to do this on Windows. Do I even need to do this?

有什么建议吗?

推荐答案

由于缓存,这种比较最终是从苹果到橘子,因为这样可以更有效地传输或在连续的内存块上进行某些工作.该特定基准是受内存限制的,因为实际上没有进行任何计算,因此缓存命中的百分比是获得良好性能的关键.

That comparison ends up being apples to oranges due to caching, because it is more efficient to transfer or do some work on contiguous chunks of memory. This particular benchmark is memory bound, since in fact no computation is done, and thus the percentage of cache hits is key to achieve good performance.

Matlab以列优先顺序(Fortran顺序)放置数据,因此a(:,:,k)是连续的内存块,可以快速复制.

Matlab lays the data in column-major order (Fortran order), so a(:,:,k) is a contiguous chunk of memory, which is fast to copy.

Numpy默认为行优先顺序(C顺序),因此在a[:,:,k]中,元素之间存在较大的跳转,这减慢了内存传输的速度.实际上,可以选择数据布局.在我的笔记本电脑中,使用a = np.asfortranarray(np.random.rand(5000,5000,3))创建阵列可以将速度提高5倍(1 s和0.19 s).

Numpy defaults to row-major order (C order), so in a[:,:,k] there are big jumps between elements and that slows down the memory transfer. Actually, the data layout can be chosen. In my laptop, creating the array with a = np.asfortranarray(np.random.rand(5000,5000,3)) leds to a 5x speed up (1 s vs 0.19 s).

对于numpy-MKL和普通numpy来说,此结果应该非常相似,因为MKL是一种快速的LAPACK实现,并且在这里您不会调用任何使用它的函数(MKL在求解线性系统,计算点积时绝对有帮助. ).

This result should be very similar both for numpy-MKL and plain numpy because MKL is a fast LAPACK implementation and here you're not calling any function that uses it (MKL definitely helps when solving linear systems, computing dot products...).

我真的不知道高斯塞德尔求解器上发生了什么,但是不久前我写了一个题为,其中谈到了MKL,FFT和Matlab的JIT.

I don't really know what's going on on the Gauss Seidel solver, but some time ago I wrote an answer to a question titled Numpy running at half the speed of MATLAB that talks a little bit about MKL, FFT and Matlab's JIT.

这篇关于MATLAB的速度是Numpy的两倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!