基于Numba提高python运行效率过程解析
作者:pfeiliu 时间:2022-09-10 11:00:38
Numba是Python的即时编译器,在使用NumPy数组和函数以及循环的代码上效果最佳。使用Numba的最常见方法是通过其装饰器集合,这些装饰器可应用于您的函数以指示Numba对其进行编译。调用Numba装饰函数时,它会被“即时”编译为机器代码以执行,并且您的全部或部分代码随后可以本机机器速度运行!
安装numba(我在这里加了--default-timeout=10000,防止安装时出现timeout的错误)
pip --default-timeout=10000 install -U numba
from numba import jit
import time
def add(x):
he=0
for i in range(x):
he+=i
return he
start=time.time()
res=add(100000000)
print(res)
end=time.time()
print(end-start)
耗时5s
4999999950000000
5.707650184631348
使用Numba的JIT装饰器@jit,以尝试加快某些功能的运行速度
from numba import jit
import time
@jit
def add(x):
he=0
for i in range(x):
he+=i
return he
start=time.time()
res=add(100000000)
print(res)
end=time.time()
print(end-start)
耗时0.14s,快了近40倍
4999999950000000
0.14488554000854492
看吧,快了40倍!
numba仅对numpy,for和while循环有效!
参考numba官网:http://numba.pydata.org/numba-doc/latest/developer/inlining.html#example-using-numba-jit
来源:https://www.cnblogs.com/pfeiliu/p/12074207.html
标签:Numba,python,运行,效率
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PHP json_encode中文乱码解决方法
2023-07-12 20:20:14
python使用参数对嵌套字典进行取值的方法
2022-04-12 10:13:27
ASP所有的Session变量获取实现代码
2011-03-11 10:44:00
Elasticsearch之倒排索引及索引操作
2023-10-28 00:38:38
django settings.py配置文件的详细介绍
2022-09-20 07:40:33
python绘制字符画视频的示例代码
2023-11-09 16:21:46
![](https://img.aspxhome.com/file/2023/7/63957_0s.jpg)
内容呈现的减法
2007-11-16 16:22:00
![](https://img.aspxhome.com/file/UploadPic/200711/16/2007111616240175s.gif)
php is_numberic函数造成的SQL注入漏洞
2023-07-18 00:32:25
![](https://img.aspxhome.com/file/2023/0/55450_0s.jpg)
python的keyword模块用法实例分析
2022-04-06 07:56:22
django-rest-framework 自定义swagger过程详解
2023-01-01 22:05:34
![](https://img.aspxhome.com/file/2023/4/72214_0s.png)
如何用css制作有趣的按钮
2008-03-17 13:54:00
![](https://img.aspxhome.com/file/UploadPic/20083/17/200831714710377s.gif)
详解PHP合并多个PDF文件的方法
2023-06-15 07:05:22
解构用户研究
2010-03-15 12:34:00
![](https://img.aspxhome.com/file/UploadPic/20103/15/deconstructing_user_research_banner730-250-33s.jpg)
使用python接入微信聊天机器人
2022-04-08 10:07:23
PHP页面静态化的优缺点与实现
2023-05-27 09:14:40
在网页设计中,如何使用图标来支持内容?[译]
2009-03-16 16:35:00
![](https://img.aspxhome.com/file/UploadPic/20093/16/280slides-85s.jpg)
基于Python的科学占卜工具开发过程
2023-01-01 03:15:05
![](https://img.aspxhome.com/file/2023/2/72092_0s.gif)
Python采集王者最低战力信息实战示例
2021-09-29 17:54:13
![](https://img.aspxhome.com/file/2023/9/72949_0s.png)
Python BeautifulReport可视化报告代码实例
2023-11-12 14:53:13
![](https://img.aspxhome.com/file/2023/4/64314_0s.png)
理解HTTP消息头
2008-12-10 14:06:00