python 多線程與多進(jìn)程效率測試
1、概述
在Python中,計算密集型任務(wù)適用于多進(jìn)程,IO密集型任務(wù)適用于多線程
正常來講,多線程要比多進(jìn)程效率更高,因為進(jìn)程間的切換需要的資源和開銷更大,而線程相對更小,但是我們使用的Python
大多數(shù)的解釋器是Cpython
,眾所周知Cpython
有個GIL鎖
,導(dǎo)致執(zhí)行計算密集型
任務(wù)時多線程實際只能是單線程
,而且由于線程之間切換的開銷導(dǎo)致多線程往往比實際的單線程還要慢,所以在 python 中計算密集型任務(wù)通常使用多進(jìn)程,因為各個進(jìn)程有各自獨立的GIL,互不干擾。
而在IO密集型
任務(wù)中,CPU時常處于等待狀態(tài),操作系統(tǒng)需要頻繁與外界環(huán)境進(jìn)行交互,如讀寫文件,在網(wǎng)絡(luò)間通信等。在這期間GIL會被釋放,因而就可以使用真正的多線程。
上面都是理論,接下來實戰(zhàn)看看實際效果是否符合理論
2、代碼練習(xí)
"""多線程多進(jìn)程模擬執(zhí)行效率""" from multiprocessing import Pool from threading import Thread import time, math def simulation_IO(a): """模擬IO操作""" time.sleep(3) def simulation_compute(a): """模擬計算密集型任務(wù)""" for i in range(int(1e7)): math.sin(40) + math.cos(40) return def normal_func(func): """普通方法執(zhí)行效率""" for i in range(6): func(i) return def mp(func): """進(jìn)程池中的map方法""" with Pool(processes=6) as p: res = p.map(func, list(range(6))) return def asy(func): """進(jìn)程池中的異步執(zhí)行""" with Pool(processes=6) as p: result = [] for j in range(6): a = p.apply_async(func, args=(j, )) result.append(a) res = [j.get() for j in result] def thread(func): """多線程方法""" threads = [] for j in range(6): t = Thread(target=func, args=(j, )) threads.append(t) t.start() for t in threads: t.join() def showtime(f, func, name): """ 計算并展示函數(shù)的運行時間 :param f: 多進(jìn)程和多線程的方法 :param func: 多進(jìn)程和多線程方法中需要傳入的函數(shù) :param name: 方法的名字 :return: """ start_time = time.time() f(func) print(f"{name} time: {time.time() - start_time:.4f}s") def main(func): """ 運行程序的主函數(shù) :param func: 傳入需要計算時間的函數(shù)名 """ showtime(normal_func, func, "normal") print() print("------ 多進(jìn)程 ------") showtime(mp, func, "map") showtime(asy, func, "async") print() print("----- 多線程 -----") showtime(thread, func, "thread") if __name__ == "__main__": print("------------ 計算密集型 ------------") func = simulation_compute main(func) print() print() print() print("------------ IO 密集型 ------------") func = simulation_IO main(func)
3、運行結(jié)果
線性執(zhí)行 | 多進(jìn)程(map) | 多進(jìn)程(async) | 多線程 | |
---|---|---|---|---|
計算密集型 | 16.0284s | 3.5236s | 3.4367s | 15.2142s |
IO密集型 | 18.0201s | 3.0945s | 3.0809s | 3.0041s |
從表格中很明顯的可以看出:
- 計算密集型任務(wù)的速度:多進(jìn)程 >多線程> 單進(jìn)程/線程
- IO密集型任務(wù)速度: 多線程 > 多進(jìn)程 > 單進(jìn)程/線程。
所以,針對計算密集型任務(wù)使用多進(jìn)程,針對IO密集型任務(wù)使用多線程
到此這篇關(guān)于python 多線程與多進(jìn)程效率測試 的文章就介紹到這了,更多相關(guān)python 多線程內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。