加速你的python腳本

因為近期要寫嵌套for循環(huán),由于運算量有點大,耗時比較久。所以就在谷歌上搜了搜有沒有辦法可以提升python for loop的速度,然后就發(fā)現(xiàn)了非常好用的模塊:Numba

image

Numba makes Python code fast

官方網(wǎng)址:http://numba.pydata.org/

首先如果你沒安裝的話,可以通過pip install numba --user裝一下,或者如果你已經(jīng)安裝了Anaconda3的話,那直接用conda安裝的python3就有這個模塊。

tips:用anaconda管理模塊、軟件,解決環(huán)境沖突問題,省時省力,附上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的用法很簡單,一般是加速某個函數(shù)。如果你想加速函數(shù)x,只需要在定義函數(shù)x的時候,在def前一行加上一個裝飾器@jit就行了(就簡單的一行代碼)。

下面以筆者寫的小例子進行介紹,這個例子主要計算a1到a2所有數(shù)的加和,并用time模塊來檢測函數(shù)的運行時間:

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)

其實能發(fā)現(xiàn)兩個函數(shù)的主體是完全一樣的,最主要的不同是在func_A1前面加了一句@jit。

運行結(jié)果如下:


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

細心的讀者可能發(fā)現(xiàn)了,我對每個函數(shù)都運行了2次,func_A的時間幾乎一致,func_A1第二次的時間比第一次少了四個數(shù)量級,這是因為第二次的時間才是numba加速后函數(shù)執(zhí)行的時間。

通俗理解,numba第一次讀取函數(shù)時,會將函數(shù)轉(zhuǎn)換為計算更快的語言,這是編譯的過程,會消耗一些時間,之后numba將編譯存儲起來,下次遇見同類型的數(shù)據(jù),直接讀取編譯,計算得到結(jié)果。官方解釋如下:

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腳本需求的人來說。

歡迎關(guān)注公眾號:"生物信息學(xué)"

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,932評論 0 13
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,872評論 0 10
  • 體態(tài)龐大、身材健碩的駱駝被稻草壓死,聽來稍顯不可思議,怎奈:“想象很骨感,現(xiàn)實很打臉?!鼻铱磯核礼橊劦淖詈?..
    最初遇見閱讀 522評論 0 2
  • 要挨多少刀板子, 才能練得一身唱念做打。 要歷經(jīng)多少世態(tài)變幻, 才顯藝術(shù)的魔力。 那些懂戲的人不懂你, 真是幸運。...
    十年007閱讀 205評論 0 11
  • 文/騎豬的書生 (一) 昨天傍晚下班,剛好出差到深圳的老友約我見了一面。 說實在,見面的那一刻我差點沒認識是他,那...
    騎豬的書生閱讀 581評論 4 21

友情鏈接更多精彩內(nèi)容