【发布时间】:2015-12-15 05:28:47
【问题描述】:
由于某种原因,numpy 的 QR 分解总是比 scipy 的快,这看起来很奇怪,因为 scipy 应该包含所有 numpy 以及更高级的功能。知道为什么吗?
import numpy.linalg as nla
import scipy.linalg as la
A = np.random.randn(4000,4000)
%timeit -n 3 -r 3 Q,R = nla.qr(A)
%timeit -n 3 -r 3 Q,R = la.qr(A)
%timeit -n 3 -r 3 Q,R = nla.qr(A)
%timeit -n 3 -r 3 Q,R = la.qr(A)
3 次循环,最好的 3 次:每个循环 1.17 秒
3 次循环,最好的 3 次:每个循环 1.21 秒
3 次循环,最好的 3 次:每个循环 1.05 秒
3 次循环,最好的 3 次:每个循环 1.21 秒
mode="raw" 的区别更明显。
%timeit -n 3 -r 3 Q = nla.qr(A, mode='raw')
%timeit -n 3 -r 3 Q = la.qr(A, mode='raw')
%timeit -n 3 -r 3 Q = nla.qr(A, mode='raw')
%timeit -n 3 -r 3 Q = la.qr(A, mode='raw')
3 个循环,3 个循环中的最佳值:每个循环 440 毫秒
3 个循环,3 个循环中的最佳值:每个循环 612 毫秒
3 个循环,3 个循环中的最佳值:每个循环 436 毫秒
3 个循环,3 个循环中的最佳值:每个循环 758 毫秒
我同时使用英特尔 MKL,并且我拥有最新的开发版本
numpy 版本:1.11.0.dev0+90a1a9f
scipy 版本:0.17.0.dev0+8003fab
NumPy config:
blas_opt_info:
library_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/lib/intel64']
include_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/include']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
libraries = ['mkl_rt', 'pthread']
mkl_info:
library_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/lib/intel64']
include_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/include']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
libraries = ['mkl_rt', 'pthread']
lapack_opt_info:
library_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/lib/intel64']
include_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/include']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
libraries = ['mkl_rt', 'pthread']
openblas_lapack_info:
NOT AVAILABLE
lapack_mkl_info:
library_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/lib/intel64']
include_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/include']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
libraries = ['mkl_rt', 'pthread']
blas_mkl_info:
library_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/lib/intel64']
include_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/include']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
libraries = ['mkl_rt', 'pthread']
SciPy config:
blas_opt_info:
libraries = ['mkl_rt', 'pthread']
include_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/include']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
library_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/lib/intel64']
mkl_info:
libraries = ['mkl_rt', 'pthread']
include_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/include']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
library_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/lib/intel64']
blas_mkl_info:
libraries = ['mkl_rt', 'pthread']
include_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/include']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
library_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/lib/intel64']
openblas_lapack_info:
NOT AVAILABLE
lapack_mkl_info:
libraries = ['mkl_rt', 'pthread']
include_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/include']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
library_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/lib/intel64']
lapack_opt_info:
libraries = ['mkl_rt', 'pthread']
include_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/include']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
library_dirs = ['/opt/intel/compilers_and_libraries_2016/linux/mkl/lib/intel64']
【问题讨论】:
-
如果 NumPy 的版本为您提供了所需的功能,请使用它。如果您需要 SciPy 的功能,请使用它。功能更全的库变慢的情况并不少见。
-
我查看了两个版本的代码。
scipy不调用或合并numpy之一。他们的mode逻辑不同,乍一看调用lapack的方法也不同。答案可能需要详细研究。