Python是目前世界上增長最快的編程語言之一,深受全球開發者的喜愛。其簡單語法和豐富的庫使得在各個領域都能得到廣泛應用,比如數據科學、機器學習、信號處理、數據可視化等。然而,Python在解決復雜問題時可能會顯得執行速度較慢。因此,本文將探討一些優化Python代碼的方法,以加速代碼運行。
Python標準庫和第三方庫(如NumPy、Pandas等)中的函數通常是用C或Cython編寫的,運行速度遠超純Python代碼。為了加速Python代碼,可以盡量使用這些庫中的向量化操作代替Python原生循環,特別是在處理數組和矩陣運算時。
舉個例子,計算Python列表中每個元素的平方。
import numpy as npimport time# 定義一個Python列表python_list = [1, 2, 3, 4, 5]# 使用純Python循環計算平方的時間測試def measure_time(function, argument): start_time = time.time() result = function(argument) end_time = time.time() return result, end_time - start_time# 定義純Python循環計算平方的函數def square_elements_python(lst): squared_lst = [] for num in lst: squared_lst.append(num ** 2) return squared_lst# 計算并輸出純Python循環方法執行時間和結果python_squares, python_time = measure_time(square_elements_python, python_list)print(f"純Python循環方法: {python_squares}, Time taken: {python_time} seconds")# 轉換為NumPy數組并使用向量化操作start_time = time.time()numpy_array = np.array(python_list)numpy_squares = numpy_array ** 2end_time = time.time()# 輸出NumPy向量化操作執行時間numpy_time = end_time - start_timeprint(f"NumPy向量化操作: {numpy_squares.tolist()}, Time taken: {numpy_time} seconds")
輸出結果如下,由此可以看出NumPy的向量化操作在執行速度上遠超純Python循環法。這是因為NumPy內部對數組操作進行了高度優化,并利用C語言編寫的底層算法,極大地提高了處理效率。
純Python循環方法: [1, 4, 9, 16, 25], Time taken: 4.5299530029296875e-06 secondsNumPy向量化操作: [1, 4, 9, 16, 25], Time taken: 0.00020122528076171875 seconds
可以使用Numba庫進行即時(JIT)編譯,它可以將指定的Python函數轉換為高效機器碼,以提升執行速度。尤其適用于數值計算密集型代碼。
例如下面的代碼,sum_array函數被裝飾器@jit(nopython=True)標記后,Numba會對其進行即時編譯,將其轉換為機器碼以提升計算密集型任務的執行速度。
import numpy as npfrom numba import jit@jit(nopython=True)def sum_array(arr: np.ndarray) -> float: result = 0.0 for i in range(arr.shape[0]): result += arr[i] return resultarr = np.array([1.0, 2.0, 3.0, 4.0, 5.0])print(sum_array(arr))
盡可能在原地修改對象,而不是創建新對象。例如,使用列表的extend()方法而非"+"操作符進行合并,使用numpy數組的切片賦值而不是重新創建數組。例如:
# 避免:list1 = [1, 2, 3]list2 = [4, 5, 6]new_list = list1 + list2# 推薦:list1 = [1, 2, 3]list2 = [4, 5, 6]# 這里不會創建新的列表對象,而是直接在原地擴展list1list1.extend(list2)
當不需要一次性生成所有結果,而是逐個處理時,使用生成器表達式代替列表推導式可以節省內存,因為它不會立即創建完整列表。
例如假設有一個包含整數的列表,我們想要計算每個整數的平方并輸出結果。使用列表推導式的方法如下:
numbers = [1, 2, 3, 4, 5]squared_numbers = [num ** 2 for num in numbers]for squared_num in squared_numbers: print(squared_num)
輸出結果為:
1491625
但是,如果我們只需要逐個處理每個平方數,而不需要將它們存儲在內存中,可以使用生成器表達式代替列表推導式:
numbers = [1, 2, 3, 4, 5]squared_numbers = (num ** 2 for num in numbers)for squared_num in squared_numbers: print(squared_num)
輸出結果與之前相同,但是使用生成器表達式可以節省內存,因為它不會一次性生成所有結果,而是逐個生成。
對于CPU密集型任務,Python的多線程受GIL限制,但對于IO密集型任務或是使用多核CPU處理CPU密集型任務時,可以通過multiprocessing庫開啟多進程來提升效率。
例如如下代碼定義了一個計算密集型函數cpu_bound_task,然后通過multiprocessing.Pool創建了與CPU核心數量相等的進程池,并用pool.map()方法將輸入列表中的任務分配給這些進程進行并行處理。
這樣,每個進程都有自己的內存空間和獨立GIL,從而可以充分利用多核處理器的能力提高執行效率。
import multiprocessing as mpdef cpu_bound_task(n): # 模擬的CPU密集型計算任務 result = 0 for i in range(n): result += i * i return resultif __name__ == "__main__": inputs = [1_000_000 + x for x in range(10)] # 多個需要處理的數據單元 with mp.Pool(processes=mp.cpu_count()) as pool: # 使用所有可用CPU核心數 results = pool.map(cpu_bound_task, inputs) # 將任務分配到各個進程中并行處理 print(f"Results: {results}")
如果存在重復計算的情況,可以使用functools.lru_cache裝飾器來緩存函數的返回結果,避免重復計算。
如下示例使用Python標準庫中的functools.lru_cache裝飾器來緩存函數的結果,避免重復計算。
from functools import lru_cache@lru_cache(maxsize=None) # 緩存所有結果,可以根據實際情況設置緩存大小def expensive_computation(x): # 假設這是一個計算成本很高的函數 print("Computing...") return x ** x# 第一次調用時會執行計算result1 = expensive_computation(5)# 第二次調用時會從緩存中獲取結果,不再執行計算result2 = expensive_computation(5)print(result1 == result2)
第一次調用expensive_computation(5)時,執行計算并打印"Computing...",然后返回計算結果25。第二次調用時,由于結果已被緩存,不再執行計算,直接返回上次計算得到的25。因此,result1 == result2的輸出是True。
在處理大量IO操作時,如網絡請求、文件讀寫等,可以利用asyncio庫實現異步編程,最大化利用等待IO完成的時間進行其他任務的處理。
例如下面例子使用Python的asyncio庫來并行處理多個網絡請求,它會同時發起10個對'http://example.com'的異步網絡請求,并等待所有請求完成后,通過responses變量獲取所有的響應結果,然后逐個調用process_response(response)函數處理這些響應。
import asyncioasync def fetch(url): # 異步網絡請求 response = await asyncio.get_event_loop().run_in_executor(None, fetch_from_network, url) return responseasync def main(): tasks = [fetch('http://example.com') for _ in range(10)] responses = await asyncio.gather(*tasks) for response in responses: process_response(response)# 啟動事件循環asyncio.run(main())
對于計算密集型的部分代碼,可以使用Cython編寫,將其編譯為C擴展模塊,或者直接使用Python的C API編寫擴展模塊,這種方式可以大幅提高這部分代碼的執行效率。示例如下:
首先,安裝Cython并創建.pyx文件:
# example_cython.pyxdef cython_power(int x): return x ** x
然后,編譯為C擴展模塊:
$ cython example_cython.pyx$ gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python3.7 -o example_cython.cpython-37m-x86_64-linux-gnu.so example_cython.c
最后,在Python中導入并使用:
import example_cython# 使用Cython優化后的函數result = example_cython.cython_power(5)print(result)
通過這種方法,Cython能夠自動將Python代碼轉化為C代碼,使得原本在Python中執行的某些計算密集型任務得以顯著加速。
本文鏈接:http://www.tebozhan.com/showinfo-26-79836-0.html加速 Python 代碼的八個優秀實用技巧
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com