为什么Matlab矩阵求逆比numpy更快? [英] Why Matlab matrix inversion is faster than numpy?

查看:619
本文介绍了为什么Matlab矩阵求逆比numpy更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与讨论numpy与Matlab的速度的几个问题有关.但是,它们中的大多数都比单个运算具有多个矩阵运算.例如. numpy和matlab之间的性能差异

This is related to several questions that discuss the speed of numpy vs Matlab. However most of them have several matrix operations than a single operation. E.g. Difference on performance between numpy and matlab

对我来说,numpy用来反转随机矩阵所需的时间比matlab慢大约5倍.

For me, the time numpy take just to invert a random matrix is approximately 5 times slower than that of matlab.

这是matlab脚本,

Here is the matlab script,

N = 1000;

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

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

这平均约为0.08秒.

This gives an average values of 0.08 seconds approximately.

尽管此python脚本给出了0.4秒(大约)的时间.

While this python script gives 0.4 seconds (approx).

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

是否有任何理由,或者无论如何要提高python性能? 我已经在Python上实现了我的工作,我担心是否 我将必须将所有代码移植到matlab. 我正在使用Ubuntu 16.04,Python 2.7,Matlab R2016b.

Is there any reason for this, or anyway to improve python performance ? I have already implemented my work on Python and I am worried whether I will have to port all my code to matlab. I am working on Ubuntu 16.04, Python 2.7, Matlab R2016b.

我已经阅读到time不是执行时间比较的好模块,我觉得这还不止于此.

I have read that the time is not a good module for execution time comparisons, I feel this is something more than that.

推荐答案

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

On my computer (Windows, python 3.5, numpy 1.11.2) :

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

或者,没有Ipython:

edit:

or, without Ipython:

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

类似于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']
...

这篇关于为什么Matlab矩阵求逆比numpy更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆