68

numba加速python学习与尝试

 5 years ago
source link: http://yongqing.is-programmer.com/posts/214028.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

简介

探索python性能优化工具,发现了numba. 只需要给函数加上装饰器就可以。比cython和pypy方便多了。

numba是什么

numba是为了提高numpy速度而开发的,使用llvm将python代码翻译为bitcode,并在bitcode外面做了一层包装,让python可以调用

通过numba翻译的代码由于经过llvm优化并可在机器上直接执行,效率将有所提高,对海量数据处理非常有帮助

numba可以解决什么问题

1.提高numpy速度
2.解决python在计算上的性能问题

原理

通过llvm编译器将python代码转换成机器码,提高执行效率。

初试

环境说明

python3.6
fedora

安装

pip install numba

示例

#斐波那契数列
import time
from numba import jit

@jit
def fib(n):
    if n<=2 :
        return 1;
    else:
        return fib(n-1)+fib(n-2);

start = time.time()
fib(50)
end = time.time()
print("python3+numba cost_seconds:", end-start)

#结果。结果很明显numba可以让你的python飞起来
python3        20.24455213546753秒
python3+numba  0.5986552238464355秒

文档

* 官方/文档

http://numba.pydata.org/

http://numba.pydata.org/numba-doc/latest/index.html

* 官方例子

https://numba.pydata.org/numba-examples/

* github

https://github.com/numba/numba

适用范围

操作系统: windows32/64, osx, linux32/64
架构: x86, x86_64, ppc64le. Experimental on armv7l, armv8l (aarch64).
GPUs:  Nvidia CUDA. Experimental on AMD ROC
CPython
Numpy1.10以上

注意要点

numba只适合解决python项目在计算上的性能问题,否则会导致项目性能下降。

引入前最好足够测试确保使用numba的姿势正确

来源

numba加速python学习与尝试

此生必看的科学实验-水知道答案

《了凡四训》详解之改过之法

印光大师十念法(胡小林主讲第1集)

精神病为什么治不好

百病之源


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK