python numba jit效率如何提高,numbajit,因为本人目前正在学习py
python numba jit效率如何提高,numbajit,因为本人目前正在学习py
因为本人目前正在学习python,其中有一个叫numba的库,使用了llvm,我也不是很了解,但是看起来好像很牛B的样子,听说效率很高。所以我看了一些它的文档,按照官方提供的例子试了一遍:
pythonfrom numba import jitfrom numpy import arangefrom datetime import datetime# jit decorator tells Numba to compile this function.# The argument types will be inferred by Numba when function is called.@jitdef sum2d(arr): M, N = arr.shape result = 0.0 for i in range(M): for j in range(N): result += arr[i,j] return resulta = arange(9).reshape(3,3)start = datetime.now()print(sum2d(a))stop = datetime.now()print(stop-start)
发现它平均需要花费53毫秒的时间,然后我将@jit注释掉,发现居然连1毫秒都不到,不是说numba效率很高吗?我想不出意外的话,肯定是我哪里弄错了,然后我试了好久,发现如果我把数组变得很大的话,numba的效率就体现了出来:
pythonfrom numba import jitfrom numpy import arangefrom datetime import datetime# jit decorator tells Numba to compile this function.# The argument types will be inferred by Numba when function is called.@jitdef sum2d(arr): M, N = arr.shape result = 0.0 for i in range(M): for j in range(N): result += arr[i,j] return resulta = arange(9999999).reshape(3333333,3)start = datetime.now()print(sum2d(a))stop = datetime.now()print(stop-start)
不使用jit需要2.56秒的时间,而使用了jit才70毫秒,这已经不是同一个数量级了,但是对于一些简单的计算使用jit反而慢了呢?
JIT是有代码预热的,同样的代码跑的次数多才能看出来优势的。
编橙之家文章,
相关内容
- 请问vmware如何从外部通过http访问内部的方法,vmware访问
- python语言为什么那么的参数函数和变量,python函数变量
- Python SDK安装遇到错误提示求助,pythonsdk,各位大牛们好
- Python2.6 pip安装gittle报错求解,python2.6gittle,python2.6 pi
- 需要用Python将编码转换成汉字求帮助,python汉字,请问,
- 关于python编辑器内提示的问题,python编辑器内提示,为什
- Python scrapy抓取淘宝数据可行吗,pythonscrapy,例如想抓取
- 这是一个关于判断浮点数的精度问题,求指导,点数精度
- python3.5 pip安装openpyxl不成功是什么原因,python3.5openpy
- python split函数返回None的疑问,pythonsplit,python split
评论关闭