开发者

Python eigenvalue computations run much slower than those of MATLAB on my computer. Why?

开发者 https://www.devze.com 2023-03-06 17:36 出处:网络
I would like to compute the eigenvalues of large-ish matrices (about 1000x1000) using Python 2.6.5. I have been unable to do so quickly. I have not found any other threads addressing this question.

I would like to compute the eigenvalues of large-ish matrices (about 1000x1000) using Python 2.6.5. I have been unable to do so quickly. I have not found any other threads addressing this question.

When I run

a = rand(1000,1000);
tic;
for i =1:10
    eig(a);
end
toc;

in MATLAB it takes about 30 seco开发者_JAVA百科nds. A similar test in Python requires 216 seconds. Running it through R using RPy did not speed up the computation appreciably. A test in Octave took 93 seconds. I am a bit baffled at the difference in speed.

The only instance of a question like this one I can find online is this, which is several years old. The poster in that question has a different Python directory structure (which I attribute to the age of the post, although I could be mistaken), so I have not been confident enough to attempt to follow the instructions posted by the correspondent.

My package manager says that I have LAPACK installed, and I am using NumPy and SciPy for the Python calculations:

from numpy import *
from scipy import *
from numpy.linalg import *
import time

a = randn(1000,1000)
tic = time.clock()
for i in range(0,10):
    eig(a)
toc = time.clock()
print "Elapsed time is ", toc-tic

I am pretty new to Python, so I may have done something silly. Please let me know if I need to provide any more information.


I think what you're seeing is the difference between the Intel Math Kernel Library (MKL) that's being used by Matlab and whatever LAPACK implementation you have on your system (ATLAS, maybe?) that scipy is linked against. You can see how much faster the MKL is in these benchmarks.

I imagine that you would get much better performance if you could rebuild Scipy against the Intel MKL libraries. If you're using Windows, pre-built copies can be downloaded from here, or you might consider using something like the Enthought Python Distribution.


I do get a difference in timings, but not as drastic as yours. My MATLAB (R2010b) timing was ~25 seconds and python (2.7) timing was ~60 seconds.

I'm not really surprised by these numbers as MATLAB is solely a numerical and matrix manipulation language, and it has the advantage of its JIT accelerator over python, which is a general purpose language. Generally, the differences between MATLAB and python+numpy are quite small, but become apparent when the matrix size is large, as in your case.

That doesn't mean there aren't ways to improve python's performance. The PerformancePython article on scipy's website gives a good introduction to the different ways in which you can improve the performance of python.

0

精彩评论

暂无评论...
验证码 换一张
取 消