MATLAB 比 Python 快吗?
我想使用 Biot–Savart 定律 计算某些导体的磁场我想使用 1000x1000x1000 矩阵。之前我使用MATLAB,但现在我想使用Python。 Python 比 MATLAB 慢吗?如何让 Python 更快?
编辑: 也许最好的方法是用 C/C++ 计算大数组,然后将它们传输到 Python。我想用 VPython 进行可视化。
EDIT2:在我的情况下哪个更好:C 或 C++?
I want to compute magnetic fields of some conductors using the Biot–Savart law and I want to use a 1000x1000x1000 matrix. Before I use MATLAB, but now I want to use Python. Is Python slower than MATLAB ? How can I make Python faster?
EDIT:
Maybe the best way is to compute the big array with C/C++ and then transfering them to Python. I want to visualise then with VPython.
EDIT2: Which is better in my case: C or C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可能会在此链接底部找到一些有用的结果
http://wiki.scipy.org/PerformancePython
从介绍来看,
它还与 MATLAB 进行了比较,并且似乎显示出与使用 Python 和 NumPy 时相似的速度。
当然这只是一个具体的例子,您的应用程序可能会允许更好或更差的性能。对两者进行相同的测试并进行比较并没有什么坏处。
您还可以使用优化库来编译 NumPy,例如 ATLAS,它提供了一些 BLAS/LAPACK例程。它们的速度应该与 MATLAB 相当。
我不确定 NumPy 下载是否已经针对它构建,但我认为如果您编译 NumPy,ATLAS 会将库调整到您的系统,
http://www.scipy.org/Installing_SciPy/Windows
该链接提供了有关 Windows 平台下所需内容的更多详细信息。
编辑:
如果您想找出 C 或 C++ 哪个性能更好,可能值得提出一个新问题。尽管从上面的链接来看,C++ 具有最佳性能。其他解决方案也非常接近,即 Pyrex、Python/Fortran(使用 f2py)和内联 C++。
我在 C++ 下做过的唯一矩阵代数是使用 MTL 并实现扩展卡尔曼筛选。但我想,本质上这取决于您使用的 LAPACK/BLAS 库以及它的优化程度。
此链接包含多种语言的面向对象数值包的列表。
http://www.oonumerics.org/oon/
You might find some useful results at the bottom of this link
http://wiki.scipy.org/PerformancePython
From the introduction,
It also compares MATLAB and seems to show similar speeds to when using Python and 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. These should be of comparable speed to 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,
http://www.scipy.org/Installing_SciPy/Windows
The link has more details on what is required under the Windows platform.
EDIT:
If you want to find out what performs better, C or C++, it might be worth asking a new question. Although from the link above C++ has best performance. Other solutions are quite close too i.e. Pyrex, Python/Fortran (using f2py) and inline C++.
The only matrix algebra under C++ I have ever done was using MTL and implementing an Extended Kalman Filter. I guess, though, in essence it depends on the libraries you are using LAPACK/BLAS and how well optimised it is.
This link has a list of object-oriented numerical packages for many languages.
http://www.oonumerics.org/oon/
NumPy 和 MATLAB 都使用底层 BLAS 标准线性代数运算的实现。一段时间以来,两者都使用 ATLAS,但现在 MATLAB 显然还提供了其他实现,例如 Intel 的 数学内核库 (MKL)。哪一种更快取决于系统以及 BLAS 实现的编译方式。您还可以使用 MKL 编译 NumPy,并且 Enthought 正在为其 Python 发行版提供 MKL 支持(请参阅他们的路线图)。这也是最近的一篇有趣的博客文章
另一方面,如果您需要更专业的操作或数据结构,那么 Python 和 MATLAB 都为您提供了各种优化方法(例如 Cython,PyCUDA,...)。
编辑:我更正了这个答案,以考虑不同的 BLAS 实现。我希望它现在能够公平地反映当前的情况。
NumPy and MATLAB both use an underlying BLAS implementation for standard linear algebra operations. For some time both used ATLAS, but nowadays MATLAB apparently also comes with other implementations like Intel's Math Kernel Library (MKL). Which one is faster by how much depends on the system and how the BLAS implementation was compiled. You can also compile NumPy with MKL and Enthought is working on MKL support for their Python distribution (see their roadmap). 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,...).
Edit: I corrected this answer to take into account different BLAS implementations. I hope it is now a fair representation of the current situation.
唯一有效的测试是对其进行基准测试。这实际上取决于您的平台是什么,以及 Biot-Savart 定律映射到 Matlab 或 NumPy/SciPy 内置运算的程度。
为了让 Python 更快,Google 正在开发 Unladen Swallow,这是一个 Python 的 JIT 编译器。可能还有其他类似的项目。
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.
As for making Python faster, Google's working on Unladen Swallow, a JIT compiler for Python. There are probably other projects like this as well.
根据您的编辑 2,我强烈建议您使用 Fortran,因为您可以利用可用的线性代数子例程(Lapack 和 Blas),并且它比 C/C++ 进行矩阵计算简单得多。
如果您更喜欢使用 C/C++ 方法,我会使用 C,因为您可能需要在简单的接口上获得原始性能(矩阵计算往往具有简单的接口和复杂的算法)。
但是,如果您决定使用 C++,则可以使用 TNT(模板数值工具包,Lapack 的 C++ 实现)。
祝你好运。
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.
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).
If, however, you decide to go with C++, you can use the TNT (the Template Numerical Toolkit, the C++ implementation of Lapack).
Good luck.
我找不到太多确切的数字来回答同样的问题,所以我自己做了测试。使用的结果、脚本和数据集均可在我的帖子 MATLAB 与 Python 振动分析速度。
长话短说,MATLAB 中的 FFT 函数比 Python 更好,但您可以进行一些简单的操作以获得可比较的结果和速度。我还发现,与 MATLAB 相比,在 Python 中导入数据更快(即使对于使用 scipy.io 的 MAT 文件也是如此)。
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.
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. I also found that importing data was faster in Python compared to MATLAB (even for MAT files using the scipy.io).
如果您只是使用 Python(与 NumPy),它可能会更慢,具体取决于您使用的部分、是否安装了优化的线性代数库以及您对如何利用 NumPy 的了解程度。
为了使其更快,您可以执行以下操作。有一个名为 Cython 的工具,它允许您向 Python 代码添加类型声明并将其转换为 Python 扩展模块C. 这会给您带来多少好处,这在一定程度上取决于您对类型声明的勤奋程度 - 如果您根本不添加任何类型,您将不会看到太多好处。 Cython 还支持 NumPy 类型,尽管这些类型比其他类型稍微复杂一些。
如果你有一块好的显卡并且愿意学习一些关于 GPU 计算的知识,PyCUDA 可以也有帮助。 (如果你没有 nvidia 显卡,我听说还有 PyOpenCL 正在开发中)。我不知道你的问题域,但如果它可以映射到 CUDA 问题,那么它应该能够很好地处理你的 10^9 元素。
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.
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 also has support for NumPy types, though these are a bit more complicated than other types.
If you have a good graphics card and are willing to learn a bit about GPU computing, PyCUDA can also help. (If you don't have an nvidia graphics card, I hear there is a PyOpenCL in the works as well). 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.
以下是 MATLAB 和 NumPy/MKL 基于一些线性代数函数:
http://dpinte.wordpress.com/2010/03/16/numpymkl-vs-matlab-performance/
点积为没那么慢;-)
And here is an updated "comparison" between MATLAB and NumPy/MKL based on some linear algebra functions:
http://dpinte.wordpress.com/2010/03/16/numpymkl-vs-matlab-performance/
The dot product is not that slow ;-)
我还想指出,Python (+NumPy) 可以通过 F2Py 模块轻松地与 Fortran 交互,这基本上可以让您在卸载到其中的代码片段上获得原生 Fortran 速度。
I would also like to point out that Python (+NumPy) can easily interface with Fortran via the F2Py module, which basically nets you native Fortran speeds on the pieces of code you offload into it.