简体   繁体   English

MATLAB 比 Python 快吗?

[英]Is MATLAB faster than Python?

I want to compute magnetic fields of some conductors using the Biot–Savart law and I want to use a 1000x1000x1000 matrix.我想使用Biot-Savart 定律计算一些导体的磁场,并且我想使用 1000x1000x1000 矩阵。 Before I use MATLAB, but now I want to use Python.在我使用 MATLAB 之前,但现在我想使用 Python。 Is Python slower than MATLAB ? Python 比 MATLAB 慢吗? How can I make Python faster?如何让 Python 更快?

EDIT: Maybe the best way is to compute the big array with C/C++ and then transfering them to Python.编辑:也许最好的方法是用 C/C++ 计算大数组,然后将它们传输到 Python。 I want to visualise then with VPython .我想用VPython可视化。

EDIT2: Which is better in my case: C or C++? EDIT2:在我的情况下哪个更好:C 还是 C++?

You might find some useful results at the bottom of this link您可能会在此链接的底部找到一些有用的结果

http://wiki.scipy.org/PerformancePython http://wiki.scipy.org/PerformancePython

From the introduction,从介绍来看,

A comparison of weave with NumPy, Pyrex, Psyco, Fortran (77 and 90) and C++ for solving Laplace's equation. weave 与 NumPy、Pyrex、Psyco、Fortran(77 和 90)和 C++ 的比较,用于求解拉普拉斯方程。

It also compares MATLAB and seems to show similar speeds to when using Python and NumPy .它还比较了 MATLAB,似乎显示出与使用 Python 和NumPy相似的速度。

Of course this is only a specific example, your application might be allow better or worse performance.当然这只是一个具体的例子,您的应用程序可能允许更好或更差的性能。 There is no harm in running the same test on both and comparing.在两者上运行相同的测试并进行比较是没有害处的。

You can also compile NumPy with optimized libraries such as ATLAS which provides some BLAS / LAPACK routines.您还可以使用优化的库来编译 NumPy,例如ATLAS ,它提供了一些BLAS / LAPACK例程。 These should be of comparable speed to MATLAB.这些应该具有与 MATLAB 相当的速度。

I'm not sure if the NumPy downloads are already built against it, but I think ATLAS will tune libraries to your system if you compile NumPy,我不确定 NumPy 下载是否已经针对它构建,但我认为如果你编译 NumPy,ATLAS 会根据你的系统调整库,

http://www.scipy.org/Installing_SciPy/Windows http://www.scipy.org/Installing_SciPy/Windows

The link has more details on what is required under the Windows platform.该链接包含有关 Windows 平台下所需内容的更多详细信息。

EDIT:编辑:

If you want to find out what performs better, C or C++, it might be worth asking a new question.如果您想找出性能更好的 C 或 C++,可能值得提出一个新问题。 Although from the link above C++ has best performance.尽管从上面的链接来看,C++ 的性能最好。 Other solutions are quite close too ie Pyrex, Python/Fortran (using f2py) and inline C++.其他解决方案也非常接近,即 Pyrex、Python/Fortran(使用 f2py)和内联 C++。

The only matrix algebra under C++ I have ever done was using MTL and implementing an Extended Kalman Filter.我做过的唯一 C++ 下的矩阵代数是使用MTL并实现扩展卡尔曼滤波器。 I guess, though, in essence it depends on the libraries you are using LAPACK/BLAS and how well optimised it is.不过,我想,本质上它取决于您使用 LAPACK/BLAS 的库以及它的优化程度。

This link has a list of object-oriented numerical packages for many languages.此链接包含许多语言的面向对象的数字包列表。

http://www.oonumerics.org/oon/ http://www.oonumerics.org/oon/

NumPy and MATLAB both use an underlying BLAS implementation for standard linear algebra operations. NumPy和 MATLAB 都使用底层BLAS实现来进行标准线性代数运算。 For some time both used ATLAS , but nowadays MATLAB apparently also comes with other implementations like Intel's Math Kernel Library (MKL).一段时间以来,两者都使用ATLAS ,但现在 MATLAB 显然还带有其他实现,例如英特尔的数学内核库(MKL)。 Which one is faster by how much depends on the system and how the BLAS implementation was compiled.哪个更快取决于系统以及 BLAS 实现的编译方式。 You can also compile NumPy with MKL and Enthought is working on MKL support for their Python distribution (see their roadmap ).您还可以使用 MKL 编译 NumPy, Enthought正在为他们的 Python 发行版提供 MKL 支持(请参阅他们的路线图)。 Here is also a recent interesting blog post about this.这也是最近一篇关于此的有趣博客文章

On the other hand, if you need more specialized operations or data structures then both Python and MATLAB offer you various ways for optimization (like Cython , PyCUDA ,...).另一方面,如果您需要更专业的操作或数据结构,那么 Python 和 MATLAB 都为您提供各种优化方法(如CythonPyCUDA等)。

Edit: I corrected this answer to take into account different BLAS implementations.编辑:我更正了这个答案以考虑不同的 BLAS 实现。 I hope it is now a fair representation of the current situation.我希望它现在可以公平地反映当前的情况。

The only valid test is to benchmark it.唯一有效的测试是对其进行基准测试。 It really depends on what your platform is, and how well the Biot-Savart Law maps to Matlab or NumPy/SciPy built-in operations.这实际上取决于您的平台是什么,以及 Biot-Savart 定律映射到 Matlab 或 NumPy/SciPy 内置操作的程度。

As for making Python faster, Google's working on Unladen Swallow, a JIT compiler for Python.至于让 Python 更快,Google 正在开发 Unladen Swallow,这是一个用于 Python 的 JIT 编译器。 There are probably other projects like this as well.可能还有其他类似的项目。

As per your edit 2, I recommend very strongly that you use Fortran because you can leverage the available linear algebra subroutines (Lapack and Blas) and it is way simpler than C/C++ for matrix computations.根据您的编辑 2,我强烈建议您使用 Fortran,因为您可以利用可用的线性代数子例程(Lapack 和 Blas),并且它比 C/C++ 更简单用于矩阵计算。

If you prefer to go with a C/C++ approach, I would use C, because you presumably need raw performance on a presumably simple interface (matrix computations tend to have simple interfaces and complex algorithms).如果您更喜欢使用 C/C++ 方法,我会使用 C,因为您可能需要在可能很简单的接口上提供原始性能(矩阵计算往往具有简单的接口和复杂的算法)。

If, however, you decide to go with C++, you can use the TNT (the Template Numerical Toolkit, the C++ implementation of Lapack).但是,如果您决定使用 C++,则可以使用 TNT(Template Numerical Toolkit,Lapack 的 C++ 实现)。

Good luck.祝你好运。

If you're just using Python (with NumPy), it may be slower, depending on which pieces you use, whether or not you have optimized linear algebra libraries installed, and how well you know how to take advantage of NumPy.如果您只是使用 Python(使用 NumPy),它可能会更慢,这取决于您使用的部分、您是否安装了优化的线性代数库以及您对如何利用 NumPy 的了解程度。

To make it faster, there are a few things you can do.为了让它更快,你可以做一些事情。 There is a tool called Cython that allows you to add type declarations to Python code and translate it into a Python extension module in C. How much benefit this gets you depends a bit on how diligent you are with your type declarations - if you don't add any at all, you won't see much of any benefit.有一个名为Cython的工具,它允许您将类型声明添加到 Python 代码并将其转换为 C 中的 Python 扩展模块。这会给您带来多少好处取决于您对类型声明的勤奋程度——如果您不这样做的话根本不添加任何东西,您不会看到任何好处。 Cython also has support for NumPy types, though these are a bit more complicated than other types. Cython 还支持 NumPy 类型,尽管这些类型比其他类型复杂一些。

If you have a good graphics card and are willing to learn a bit about GPU computing, PyCUDA can also help.如果你有一块好显卡,并且愿意学习一点 GPU 计算方面的知识, PyCUDA也可以提供帮助。 (If you don't have an nvidia graphics card, I hear there is a PyOpenCL in the works as well). (如果您没有 nvidia 显卡,我听说还有一个 PyOpenCL 正在开发中)。 I don't know your problem domain, but if it can be mapped into a CUDA problem then it should be able to handle your 10^9 elements nicely.我不知道您的问题域,但如果它可以映射到 CUDA 问题,那么它应该能够很好地处理您的 10^9 元素。

And here is an updated "comparison" between MATLAB and NumPy / MKL based on some linear algebra functions:这是基于一些线性代数函数的 MATLAB 和NumPy / MKL之间的更新“比较”:

http://dpinte.wordpress.com/2010/03/16/numpymkl-vs-matlab-performance/ http://dpinte.wordpress.com/2010/03/16/numpymkl-vs-matlab-performance/

The dot product is not that slow ;-)点积并没有那么慢;-)

I couldn't find much hard numbers to answer this same question so I went ahead and did the testing myself.我找不到太多难以回答的数字来回答同样的问题,所以我继续自己进行了测试。 The results, scripts, and data sets used are all available here on my post on MATLAB vs Python speed for vibration analysis .使用的结果、脚本和数据集都可以在我关于MATLAB 与 Python 速度进行振动分析的帖子中找到。

Long story short, the FFT function in MATLAB is better than Python but you can do some simple manipulation to get comparable results and speed.长话短说,MATLAB 中的 FFT 函数比 Python 更好,但您可以进行一些简单的操作来获得可比较的结果和速度。 I also found that importing data was faster in Python compared to MATLAB (even for MAT files using the scipy.io).我还发现,与 MATLAB 相比,在 Python 中导入数据更快(即使是使用 scipy.io 的 MAT 文件)。

我还想指出,Python (+NumPy) 可以通过 F2Py 模块轻松地与 Fortran 交互,这基本上可以让您在卸载到其中的代码片段上获得原生 Fortran 速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM