因为近期要写嵌套for循环,由于运算量有点大,耗时比较久。所以就在谷歌上搜了搜有没有办法可以提升python for loop的速度,然后就发现了非常好用的模块:Numba
Numba makes Python code fast
官方网址:http://numba.pydata.org/
# 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
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
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还提供了向量化运算的装饰器@vectorize,结合向量化运算使所有元素的计算同时进行,下期我将给大家继续介绍它的简单用法。