神剑山庄资源网 Design By www.hcban.com
有时候需要比较大的计算量,这个时候Python的效率就很让人捉急了,此时可以考虑使用numba 进行加速,效果提升明显~
(numba 安装貌似很是繁琐,建议安装Anaconda,里面自带安装好各种常用科学计算库)
from numba import jit @jit def t(count=1000): total = 0 for i in range(int(count)): total += i return total
测试效果:
(关于__wrapped__ 见我的博文: 浅谈解除装饰器作用(python3新增) )
In [17]: %timeit -n 1 t.__wrapped__() 1 loop, best of 3: 52.9 µs per loop In [18]: %timeit -n 1 t() The slowest run took 13.00 times longer than the fastest. This could mean that an intermediate result is being cached. 1 loop, best of 3: 395 ns per loop
可以看到使用jit 加速后,即使设置测试一次,实际上还是取了三次的最优值,如果取最坏值(因为最优值可能是缓存下来的),则耗时为395ns * 13 大概是5us 还是比不使用的52.9us 快上大概10倍,
增大计算量可以看到使用numba加速后的效果提升更加明显,
In [19]: %timeit -n 10 t.__wrapped__(1e6) 10 loops, best of 3: 76.2 ms per loop In [20]: %timeit -n 1 t(1e6) The slowest run took 8.00 times longer than the fastest. This could mean that an intermediate result is being cached. 1 loop, best of 3: 790 ns per loop
如果减少计算量,可以看到当降到明显小值时,使用加速后的效果(以最差计)与不加速效果差距不大,因此如果涉及到较大计算量不妨使用jit 加速下,何况使用起来这么简便。
%timeit -n 1 t(10) 1 loop, best of 3: 0 ns per loop %timeit -n 100 t.__wrapped__(10) 100 loops, best of 3: 1.79 µs per loop %timeit -n 1 t(1) The slowest run took 17.00 times longer than the fastest. This could mean that an intermediate result is being cached. 1 loop, best of 3: 395 ns per loop %timeit -n 100 t.__wrapped__(1) 100 loops, best of 3: 671 ns per loop
以上这篇使用numba对Python运算加速的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无使用numba对Python运算加速的方法的评论...
更新日志
2024年10月02日
2024年10月02日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】