Python中單執行緒、多執行緒和多程序的效率對比實驗
阿新 • • 發佈:2018-12-18
Python是執行在直譯器中的語言,查詢資料知道,python中有一個全域性鎖(GIL),在使用多程序(Thread)的情況下,不能發揮多核的優勢。而使用多程序(Multiprocess),則可以發揮多核的優勢真正地提高效率。
對比實驗
資料顯示,如果多執行緒的程序是CPU密集型的,那多執行緒並不能有多少效率上的提升,相反還可能會因為執行緒的頻繁切換,導致效率下降,推薦使用多程序;如果是IO密集型,多執行緒程序可以利用IO阻塞等待時的空閒時間執行其他執行緒,提升效率。所以我們根據實驗對比不同場景的效率
作業系統 | CPU | 記憶體 | 硬碟 |
---|---|---|---|
Windows 10 | 雙核 | 8GB | 機械硬碟 |
(1)引入所需要的模組
1 2 3 4 |
import requests
import time |
(2)定義CPU密集的計算函式
Python
1 2 3 4 5 6 7 | def count(x, y): # 使程式完成150萬計算 c = 0 while c < 500000: c += 1 x += x y += y |
(3)定義IO密集的檔案讀寫函式
1 2 3 4 5 6 7 8 9 10 | def write(): f = open("test.txt", "w") for x in range(5000000): f.write("testwrite\n") f.close() def read(): f = open("test.txt", "r") lines = f.readlines() f.close() |
(4) 定義網路請求函式
1 2 3 4 5 6 7 8 9 10 | _head = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36'} url = "http://www.tieba.com" def http_request(): try: webPage = requests.get(url, headers=_head) html = webPage.text return {"context": html} except Exception as e: return {"error": e} |
(5)測試線性執行IO密集操作、CPU密集操作所需時間、網路請求密集型操作所需時間
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # CPU密集操作 t = time.time() for x in range(10): count(1, 1) print("Line cpu", time.time() - t) # IO密集操作 t = time.time() for x in range(10): write() read() print("Line IO", time.time() - t) # 網路請求密集型操作 t = time.time() for x in range(10): http_request() print("Line Http Request", time.time() - t) |
輸出
- CPU密集:95.6059999466、91.57099986076355 92.52800011634827、 99.96799993515015
- IO密集:24.25、21.76699995994568、21.769999980926514、22.060999870300293
- 網路請求密集型: 4.519999980926514、8.563999891281128、4.371000051498413、4.522000074386597、14.671000003814697
(6)測試多執行緒併發執行CPU密集操作所需時間
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | counts = [] t = time.time() for x in range(10): thread = Thread(target=count, args=(1,1)) counts.append(thread) thread.start() e = counts.__len__() while True: for th in counts: if not th.is_alive(): e -= 1 if e <= 0: break print(time.time() - t) |
Output: 99.9240000248 、101.26400017738342、102.32200002670288
(7)測試多執行緒併發執行IO密集操作所需時間
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | def io(): write() read() t = time.time() ios = [] t = time.time() for x in range(10): thread = Thread(target=count, args=(1,1)) ios.append(thread) thread.start() e = ios.__len__() while True: for th in ios: if not th.is_alive(): e -= 1 if e <= 0: break print(time.time() - t) |
Output: 25.69700002670288、24.02400016784668
(8)測試多執行緒併發執行網路密集操作所需時間
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | t = time.time() ios = [] t = time.time() for x in range(10): thread = Thread(target=http_request) ios.append(thread) thread.start() e = ios.__len__() while True: for th in ios: if not th.is_alive(): e -= 1 if e <= 0: break print("Thread Http Request", time.time() - t) |
Output: 0.7419998645782471、0.3839998245239258、0.3900001049041748
(9)測試多程序併發執行CPU密集操作所需時間
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | counts = [] t = time.time() for x in range(10): process = Process(target=count, args=(1,1)) counts.append(process) process.start() e = counts.__len__() while True: for th in counts: if not th.is_alive(): e -= 1 if e <= 0: break print("Multiprocess cpu", time.time() - t) |