简体   繁体   English

为什么Matlab矩阵求逆比numpy更快?

[英]Why Matlab matrix inversion is faster than numpy?

This is related to several questions that discuss the speed of numpy vs Matlab. 这与讨论numpy与Matlab的速度的几个问题有关。 However most of them have several matrix operations than a single operation. 但是,它们中的大多数都比单个运算具有多个矩阵运算。 Eg Difference on performance between numpy and matlab 例如numpy和matlab之间的性能差异

For me, the time numpy take just to invert a random matrix is approximately 5 times slower than that of matlab. 对我来说,numpy用来反转随机矩阵所需的时间比matlab慢大约5倍。

Here is the matlab script, 这是matlab脚本,

N = 1000;

B = randn(N,N);
h = tic;
T = 40;
for i=1:40

    Rinv = (B)^(-1);
end
toc(h)/40

This gives an average values of 0.08 seconds approximately. 这样得出的平均值约为0.08秒。

While this python script gives 0.4 seconds (approx). 尽管此python脚本给出了0.4秒(大约)的时间。

import numpy as np 
from numpy import linalg as LA
import time 

N=1000
R = np.random.random((N,N))
T=40

t1 = time.clock()
for i in range(0,T):
    Rinv = LA.inv(R)
t2 = time.clock()
print 'avg time for inverse ',(t2-t1)/T

Is there any reason for this, or anyway to improve python performance ? 是否有任何原因,或者无论如何要提高python性能? I have already implemented my work on Python and I am worried whether I will have to port all my code to matlab. 我已经在Python上实现了我的工作,我担心是否必须将所有代码移植到matlab。 I am working on Ubuntu 16.04, Python 2.7, Matlab R2016b. 我正在使用Ubuntu 16.04,Python 2.7,Matlab R2016b。

I have read that the time is not a good module for execution time comparisons, I feel this is something more than that. 我读到time不是执行时间比较的好模块,我觉得这还不止于此。

On my computer (Windows, python 3.5, numpy 1.11.2) : 在我的计算机上(Windows,python 3.5,numpy 1.11.2):

In [6]: %timeit inv(a)
10 loops, best of 3: 86 ms per loop

edit: 编辑:

or, without Ipython: 或者,没有Ipython:

>>>timeit.timeit('inv(a)','from __main__ import inv,a',number=100)/100

which is similar to Matlab. 这类似于Matlab。

to know what code is used in the background, check it : 要知道在后台使用了什么代码,请检查一下:

In [12]: np.__config__.show() 
blas_mkl_info:
include_dirs = ['c:/users/bruno/miniconda3\\Library\\include']
libraries = ['mkl_core_dll', 'mkl_intel_lp64_dll', 'mkl_intel_thread_dll']
...

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

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