Python加速——NumPy

由于之前写的python代码用NumPy 优化之后,速度是嗖嗖地提升,所以就想对于NumPy 这个神器好好研究一番。

以下是Numpy的官方文档中的介绍:

NumPy is the fundamental package for scientific computing with Python. It contains among other things:

(1)a powerful N-dimensional array object
(2)sophisticated (broadcasting) functions
(3)tools for integrating C/C++ and Fortran code
(4)useful linear algebra, Fourier transform, and random number capabilities

Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases.

        也就是说,NumPy 是一个类似于MATLAB的一个存储和处理大型矩阵的库,其底层使用了C/C++/Fortran,能用来处理线性代数相关操作、傅里叶变换、随机数生成函数,以及具有广播机制

一、为什么能实现加速

        我尝试去找NumPy 的底层代码,发现是调用了.pyd文件,也就是说一些核心代码是调用了C程序。并且,NumPy 的很多代码还用到了BLAS(基础线性代数子程序库,应该是用到了汇编)(一般Windows下link到MKL的,Linux下link到OpenBLAS),基本在每种操作上都实现了高度优化。

        总结来说,就是底层是C语言,算法比较先进,编程上也进行了优化。

二、如何使用

        推荐看一本书《利用python进行数据分析》,讲的还是比较详细。https://zhuanlan.zhihu.com/p/25770302

猜你喜欢

转载自blog.csdn.net/qq_28726333/article/details/80253639