温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么加速python脚本

发布时间:2021-09-04 21:04:26 来源:亿速云 阅读:154 作者:chen 栏目:大数据

本篇内容主要讲解“怎么加速python脚本”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么加速python脚本”吧!

因为近期要写嵌套for循环,由于运算量有点大,耗时比较久。所以就在谷歌上搜了搜有没有办法可以提升python for loop的速度,然后就发现了非常好用的模块:Numba
   

Numba makes Python code fast

官方网址:http://numba.pydata.org/

首先如果你没安装的话,可以通过pip install numba --user装一下,或者如果你已经安装了Anaconda3的话,那直接用conda安装的python3就有这个模块。

tips:用anaconda管理模块、软件,解决环境冲突问题,省时省力,附上linux上的安装小教程
# download from tsinghua mirror sitewget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.3.1-Linux-x86_64.sh# check the help messagebash Anaconda3-5.3.1-Linux-x86_64.sh -h# then install or install into Nonexistent Custom Directory by adding -pbash Anaconda3-5.3.1-Linux-x86_64.sh# add to the environmentecho ". /home/saber/anaconda3/etc/profile.d/conda.sh" >> ~/.bashrc

Numba的用法很简单,一般是加速某个函数。如果你想加速函数x,只需要在定义函数x的时候,在def前一行加上一个装饰器@jit就行了(就简单的一行代码)。

下面以笔者写的小例子进行介绍,这个例子主要计算a1到a2所有数的加和,并用time模块来检测函数的运行时间:

from numba import jitimport time
#define function A without numbadef func_A(a1,a2): A_result=0 for i in range(a1,a2):  A_result+=i return A_result
#define func A1 with numba#just add the @jit@jitdef func_A1(a1,a2): A1_result=0 for i in range(a1,a2):  A1_result+=i return A1_result
#record the elasped timedef time_func(func_A_i,*args): start = time.time() func_A_i(*args) end = time.time() print("Elasped time of func %s is %.4e"%(func_A_i.__name__,end-start))

time_func(func_A,1,10000000)time_func(func_A,1,10000000)print()time_func(func_A1,1,10000000)time_func(func_A1,1,10000000)

其实能发现两个函数的主体是完全一样的,最主要的不同是在func_A1前面加了一句@jit。

运行结果如下:

Elasped time of func func_A is 5.4757e-01Elasped time of func func_A is 5.3267e-01
Elasped time of func func_A1 is 5.3686e-02Elasped time of func func_A1 is 4.7684e-06

细心的读者可能发现了,我对每个函数都运行了2次,func_A的时间几乎一致,func_A1第二次的时间比第一次少了四个数量级,这是因为第二次的时间才是numba加速后函数执行的时间。

通俗理解,numba第一次读取函数时,会将函数转换为计算更快的语言,这是编译的过程,会消耗一些时间,之后numba将编译存储起来,下次遇见同类型的数据,直接读取编译,计算得到结果。官方解释如下:

First, recall that Numba has to compile your function for the argument types given before it executes the machine code version of your function, this takes time. However, once the compilation has taken place Numba caches the machine code version of your function for the particular types of arguments presented. If it is called again the with same types, it can reuse the cached version instead of having to compile again.

所以总的来说numba加速后速度提升还是很大的,特别是对有想加速python脚本需求的人来说。

到此,相信大家对“怎么加速python脚本”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI