加速你的python脚本

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

image

Numba makes Python code fast

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

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

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

# download from tsinghua mirror site
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.3.1-Linux-x86_64.sh
# check the help message
bash Anaconda3-5.3.1-Linux-x86_64.sh -h
# then install or install into Nonexistent Custom Directory by adding -p
bash Anaconda3-5.3.1-Linux-x86_64.sh
# add to the environment
echo ". /home/saber/anaconda3/etc/profile.d/conda.sh" >> ~/.bashrc

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

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

from numba import jit
import time

#define function A without numba
def 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
@jit
def func_A1(a1,a2):
 A1_result=0
 for i in range(a1,a2):
  A1_result+=i
 return A1_result

#record the elasped time
def 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-01
Elasped time of func func_A is 5.3267e-01

Elasped time of func func_A1 is 5.3686e-02
Elasped 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脚本需求的人来说。

欢迎关注公众号:"生物信息学"

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,503评论 0 13
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,424评论 0 10
  • 体态庞大、身材健硕的骆驼被稻草压死,听来稍显不可思议,怎奈:“想象很骨感,现实很打脸。”且看压死骆驼的最后...
    最初遇见阅读 400评论 0 2
  • 要挨多少刀板子, 才能练得一身唱念做打。 要历经多少世态变幻, 才显艺术的魔力。 那些懂戏的人不懂你, 真是幸运。...
    十年007阅读 185评论 0 11
  • 文/骑猪的书生 (一) 昨天傍晚下班,刚好出差到深圳的老友约我见了一面。 说实在,见面的那一刻我差点没认识是他,那...
    骑猪的书生阅读 463评论 4 13