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

标签 python matlab performance numpy matrix

这与讨论 numpy 与 Matlab 速度的几个问题有关。然而,它们中的大多数具有多个矩阵运算而不是单个运算。例如。 Difference on performance between numpy and matlab

对我而言,numpy 仅用于反转随机矩阵所花费的时间比 matlab 慢大约 5 倍。

这是matlab脚本,

N = 1000;

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

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

这给出了大约 0.08 秒的平均值。

虽然这个 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

是否有任何原因,或者无论如何提高 python 性能? 我已经在 Python 上实现了我的工作,我担心是否 我将不得不将我所有的代码移植到 matlab。 我正在使用 Ubuntu 16.04、Python 2.7、Matlab R2016b。

我读到 time 不是执行时间比较的好模块,我觉得这不止于此。

最佳答案

在我的电脑上(Windows、python 3.5、numpy 1.11.2):

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

编辑:

或者,没有 Ipython:

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

类似于 Matlab。

要知道后台使用了什么代码,检查它:

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']
...

关于python - 为什么 Matlab 矩阵求逆比 numpy 快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41793670/

相关文章:

python - Python 中 Matlab 'fscanf' 的等价物是什么?

c++ - 为什么 std::make_shared<>() 比 boost::make_shared() 有更好的性能?

c++ - 错误 LNK2019 : unresolved external symbol

performance - 从头开始合并列表列表

performance - Applicative functor vs monad 在 Scala 中的组合性能

python - 如何在 Flask-RESTless 中使用关键字参数

python - 如何跟踪 Tkinter Spinbox 的函数

python - 如何使用 Python 清空文件

python - 根据每场比赛的结果追加排名

matlab - 在 MATLAB 中消除警告消息