【问题标题】:Numpy/Python performing vs. MatlabNumpy/Python 与 Matlab 的对比
【发布时间】:2012-04-03 17:48:47
【问题描述】:

我在 python (numpy) 中有一个简单的矩阵乘法代码

import numpy as np
import time
a = np.random.random((70000,3000));
b = np.random.random((3000,100));
t1=time.time()
c = np.dot(a,b);
t2=time.time()
print 'Time passed is %2.2f seconds' %(t2-t1

在一个核上完成乘法 (c = np.dot(a,b);) 大约需要 16 秒。但是,当我在 Matab 上运行相同的乘法时,它需要大约 1 秒(6 核)才能完成乘法。

那么,为什么 Matlab 的矩阵乘法比 numpy 快 2.6 倍? (每个核心的性能对我来说很重要)

更新这次我使用 Eigen 尝试了同样的事情。并且它的性能略好于Matlab。 Eigen 使用与 Numpy 相同的 Blas 实现。所以 Blas 实现并不是性能缺陷的根源。

为了确保安装的 numpy 使用了 BLAS,我 np.show_config()

enter code here
blas_info:
   libraries = ['blas']
   library_dirs = ['/usr/lib64']
   language = f77
lapack_info:
   libraries = ['lapack']
   library_dirs = ['/usr/lib64']
   language = f77

atlas_threads_info:
   NOT AVAILABLE

blas_opt_info:
   libraries = ['blas']
   library_dirs = ['/usr/lib64']
   language = f77
   define_macros = [('NO_ATLAS_INFO', 1)]

atlas_blas_threads_info:
   NOT AVAILABLE

lapack_opt_info:
   libraries = ['lapack', 'blas']
   library_dirs = ['/usr/lib64']
   language = f77
   define_macros = [('NO_ATLAS_INFO', 1)]

atlas_info:
   NOT AVAILABLE

lapack_mkl_info:
   NOT AVAILABLE

blas_mkl_info:
   NOT AVAILABLE

atlas_blas_info:
   NOT AVAILABLE

mkl_info:
   NOT AVAILABLE

【问题讨论】:

  • 你是如何安装 Numpy 的?也许来自 Ubuntu 软件包?
  • 那个 blas 是来自 netlib 的参考 blas - 最慢的 blas。安装 atlas 或 mkl。
  • 是的,我用过 sudo apt-get install python-numpy
  • @iampat:众所周知,这是一个非常不理想的 Numpy 构建。安装更好的 BLAS 库后自己编译,或者使用EPD
  • @iampat - 你确定 Eigen 链接到同一个 blas 实现吗?例如,在我的机器上,链接到 netlib 的 blas 的 repo 版本的 numpy 大约需要 50 秒。使用链接到 MKL 的 blas 实现的 EPD,计算需要 0.7 秒。唯一的区别是 blas 实现。

标签: python matlab numpy


【解决方案1】:

试试Enthought Python Distribution。一方面,它链接到Intel Math Kernel Library,这是经过高度优化并被 MatLab 使用的。

编辑:2017 年更新。Anaconda distribution 确实是这些天要走的路。

【讨论】:

  • 谢谢您的回答。我无法更改我的 python 发行版。但是,我可以实现 blas 。
  • 这次我使用 Eigen 尝试了同样的事情。并且它的性能略好于Matlab。 Eigen 使用与 Numpy 相同的 Blas 实现。所以 Blas 实现并不是性能缺陷的根源。
猜你喜欢
  • 1970-01-01
  • 2015-07-16
  • 2013-07-16
  • 1970-01-01
  • 2014-10-03
  • 2018-09-29
  • 2015-08-25
  • 2013-08-18
  • 1970-01-01
相关资源
最近更新 更多