前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >加速你的python脚本

加速你的python脚本

作者头像
阿凡亮
发布2020-04-13 13:32:36
8870
发布2020-04-13 13:32:36
举报
文章被收录于专栏:生物信息学生物信息学

嗨,大家好,今天给大家安利一个模块。

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

代码语言:javascript
复制
Numba makes Python code fast

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

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

tips:用anaconda管理模块、软件,解决环境冲突问题,省时省力,附上linux上的安装小教程

代码语言:javascript
复制
# 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。

运行结果如下:

代码语言:javascript
复制
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脚本需求的人来说。

最后,其实numba还提供了向量化运算的装饰器@vectorize,结合向量化运算使所有元素的计算同时进行,下期我将给大家继续介绍它的简单用法。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-10-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生物信息学 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 嗨,大家好,今天给大家安利一个模块。
  • 首先如果你没安装的话,可以通过pip install numba --user装一下,或者如果你已经安装了Anaconda3的话,那直接用conda安装的python3就有这个模块。
    • tips:用anaconda管理模块、软件,解决环境冲突问题,省时省力,附上linux上的安装小教程
    • Numba的用法很简单,一般是加速某个函数。如果你想加速函数x,只需要在定义函数x的时候,在def前一行加上一个装饰器@jit就行了(就简单的一行代码)。
    • 下面以笔者写的小例子进行介绍,这个例子主要计算a1到a2所有数的加和,并用time模块来检测函数的运行时间:
    • 其实能发现两个函数的主体是完全一样的,最主要的不同是在func_A1前面加了一句@jit。
    • 运行结果如下:
    • 细心的读者可能发现了,我对每个函数都运行了2次,func_A的时间几乎一致,func_A1第二次的时间比第一次少了四个数量级,这是因为第二次的时间才是numba加速后函数执行的时间。
    • 通俗理解,numba第一次读取函数时,会将函数转换为计算更快的语言,这是编译的过程,会消耗一些时间,之后numba将编译存储起来,下次遇见同类型的数据,直接读取编译,计算得到结果。官方解释如下:
    • 所以总的来说numba加速后速度提升还是很大的,特别是对有想加速python脚本需求的人来说。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档