要理解為什么 Python 代碼在函數(shù)中運(yùn)行得更快,我們需要首先了解 Python 是如何執(zhí)行代碼的NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
我們知道,python 是一種解釋型語(yǔ)言,它會(huì)逐行讀取并執(zhí)行代碼NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
當(dāng)運(yùn)行一個(gè) python 程序的時(shí)候,首先將代碼編譯成字節(jié)碼(一種更接近機(jī)器碼的中間語(yǔ)言)然后 python 解釋器執(zhí)行字節(jié)碼NPr28資訊網(wǎng)——每日最新資訊28at.com
圖片NPr28資訊網(wǎng)——每日最新資訊28at.com
圖片NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
由上所示,python 中的 dis 模塊將函數(shù) hello_world 分解為字節(jié)碼NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
需要注意的是,python 解釋器是一個(gè)執(zhí)行字節(jié)碼的虛擬機(jī),默認(rèn)的 python 解釋器是用 C 編寫的,即 CPythonNPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
還有其他的 python 解釋器如 Jython(用 Java 編寫),IronPython(用于 )和PyPy(用 Python 和 C 編寫)NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
為什么 python 代碼在函數(shù)中運(yùn)行得更快
NPr28資訊網(wǎng)——每日最新資訊28at.com
我們來(lái)編寫一個(gè)簡(jiǎn)單的例子:定義一個(gè)函數(shù) my_function,函數(shù)內(nèi)部包含一個(gè) for 循環(huán)NPr28資訊網(wǎng)——每日最新資訊28at.com
圖片NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
編譯該函數(shù)的時(shí)候,字節(jié)碼可能如下所示NPr28資訊網(wǎng)——每日最新資訊28at.com
圖片NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
這里的關(guān)鍵指令是 STORE_FAST ,用于存儲(chǔ)循環(huán)變量 i NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
現(xiàn)在我們把這個(gè) for 循環(huán)放在 python 腳本的頂層(全局范圍內(nèi)),然后再來(lái)看一下字節(jié)碼NPr28資訊網(wǎng)——每日最新資訊28at.com
圖片NPr28資訊網(wǎng)——每日最新資訊28at.com
圖片NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
可以看到關(guān)鍵指令變成了 STORE_NAME,而不是 STORE_FAST NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
字節(jié)碼 STORE_FAST比 STORE_NAME 快,因?yàn)樵诤瘮?shù)中,局部變量存儲(chǔ)在固定長(zhǎng)度的數(shù)組中,而不是存儲(chǔ)在字典中。這個(gè)數(shù)組可以通過(guò)索引直接訪問(wèn),使得變量檢索非常快NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
基本上,它只是一個(gè)指向列表的指針,并增加了 PyObject 的引用計(jì)數(shù),這兩個(gè)都是高效的操作NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
另一方面,全局變量存儲(chǔ)在一個(gè)字典。當(dāng)訪問(wèn)全局變量時(shí),Python 必須執(zhí)行哈希表查找,這涉及計(jì)算哈希值,然后檢索與之關(guān)聯(lián)的值NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
雖然經(jīng)過(guò)優(yōu)化,但仍然比基于索引的查找慢NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
基準(zhǔn)測(cè)試驗(yàn)證
NPr28資訊網(wǎng)——每日最新資訊28at.com
我們知道在 Python 中,代碼執(zhí)行的速度取決于代碼執(zhí)行的位置——在函數(shù)中還是在全局作用域中NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
讓我們用一個(gè)簡(jiǎn)單的基準(zhǔn)測(cè)試的例子來(lái)比較一下NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
首先定義一個(gè)求階乘的函數(shù)NPr28資訊網(wǎng)——每日最新資訊28at.com
圖片NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
然后在全局范圍內(nèi)執(zhí)行相同的代碼NPr28資訊網(wǎng)——每日最新資訊28at.com
圖片NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
為了對(duì)這兩段代碼進(jìn)行基準(zhǔn)測(cè)試,我們可以在 Python 中使用 timeit 模塊,它提供了一種簡(jiǎn)單的方法來(lái)對(duì)少量 Python 代碼進(jìn)行計(jì)時(shí)NPr28資訊網(wǎng)——每日最新資訊28at.com
圖片NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
可以看到,函數(shù)代碼的執(zhí)行速度比全局作用域代碼要快NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
需要注意的是,這兩段代碼最好不要放在同一腳本中,要分開(kāi)單獨(dú)運(yùn)行NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
這是因?yàn)?nbsp;benchmark() 函數(shù)在執(zhí)行時(shí)間上增加了一些開(kāi)銷,并且全局代碼在內(nèi)部進(jìn)行了優(yōu)化NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
cProfile 分析NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
python 提供了一個(gè) cProfile 內(nèi)置模塊NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
讓我們用它來(lái)分析一個(gè)新例子:在局部和全局范圍內(nèi)計(jì)算平方和NPr28資訊網(wǎng)——每日最新資訊28at.com
圖片NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
上面的例子中,可以認(rèn)為sum_of_squares_g() 函數(shù)是全局的,因?yàn)樗褂昧藘蓚€(gè)全局變量, i 和 totalNPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
從性能分析結(jié)果中,可以看到函數(shù)代碼在執(zhí)行時(shí)間方面比全局更有效NPr28資訊網(wǎng)——每日最新資訊28at.com
圖片NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
如何優(yōu)化 python 函數(shù)的性能
NPr28資訊網(wǎng)——每日最新資訊28at.com
前面我們知道,Python 代碼在函數(shù)中運(yùn)行往往比在全局范圍內(nèi)運(yùn)行要快得多NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
如果想要進(jìn)一步提高 python 函數(shù)代碼效率,不妨考慮一下使用局部變量而不是全局變量NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
另一種方法是盡可能使用內(nèi)置函數(shù)和庫(kù)。Python 的內(nèi)置函數(shù)是用 C 實(shí)現(xiàn)的,比 Python 快得多NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
比如 NumPy 和 Pandas,也是用 C 或 C++ 實(shí)現(xiàn)的,它們比實(shí)現(xiàn)同樣功能的 Python 代碼速度更快NPr28資訊網(wǎng)——每日最新資訊28at.com
NPr28資訊網(wǎng)——每日最新資訊28at.com
又比如同樣是實(shí)現(xiàn)數(shù)字求和的功能,python 內(nèi)置的 sum 函數(shù)要比你自己編寫函數(shù)速度更快NPr28資訊網(wǎng)——每日最新資訊28at.com
本文鏈接:http://www.tebozhan.com/showinfo-26-10609-0.html為什么 Python 代碼在函數(shù)中運(yùn)行得更快?
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com
上一篇: 使用二進(jìn)制字面量在現(xiàn)代C++中的應(yīng)用
下一篇: 數(shù)據(jù)組合利器:從入門到精通Python中的zip()函數(shù)應(yīng)用