Python中單線程、多線程和多進(jìn)程的效率對(duì)比實(shí)驗(yàn)實(shí)例
python的多進(jìn)程性能要明顯優(yōu)于多線程,因?yàn)閏python的GIL對(duì)性能做了約束。
Python是運(yùn)行在解釋器中的語(yǔ)言,查找資料知道,python中有一個(gè)全局鎖(GIL),在使用多進(jìn)程(Thread)的情況下,不能發(fā)揮多核的優(yōu)勢(shì)。而使用多進(jìn)程(Multiprocess),則可以發(fā)揮多核的優(yōu)勢(shì)真正地提高效率。
對(duì)比實(shí)驗(yàn)
資料顯示,如果多線程的進(jìn)程是CPU密集型的,那多線程并不能有多少效率上的提升,相反還可能會(huì)因?yàn)榫€程的頻繁切換,導(dǎo)致效率下降,推薦使用多進(jìn)程;如果是IO密集型,多線程進(jìn)程可以利用IO阻塞等待時(shí)的空閑時(shí)間執(zhí)行其他線程,提升效率。所以我們根據(jù)實(shí)驗(yàn)對(duì)比不同場(chǎng)景的效率
| 操作系統(tǒng) | CPU | 內(nèi)存 | 硬盤 |
|---|---|---|---|
| Windows 10 | 雙核 | 8GB | 機(jī)械硬盤 |
(1)引入所需要的模塊
import requests import time from threading import Thread from multiprocessing import Process
(2)定義CPU密集的計(jì)算函數(shù)
def count(x, y):
# 使程序完成150萬(wàn)計(jì)算
c = 0
while c < 500000:
c += 1
x += x
y += y
(3)定義IO密集的文件讀寫函數(shù)
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) 定義網(wǎng)絡(luò)請(qǐng)求函數(shù)
_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)測(cè)試線性執(zhí)行IO密集操作、CPU密集操作所需時(shí)間、網(wǎng)絡(luò)請(qǐng)求密集型操作所需時(shí)間
# 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)
# 網(wǎng)絡(luò)請(qǐng)求密集型操作
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
網(wǎng)絡(luò)請(qǐng)求密集型: 4.519999980926514、8.563999891281128、4.371000051498413、4.522000074386597、14.671000003814697
(6)測(cè)試多線程并發(fā)執(zhí)行CPU密集操作所需時(shí)間
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)測(cè)試多線程并發(fā)執(zhí)行IO密集操作所需時(shí)間
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)測(cè)試多線程并發(fā)執(zhí)行網(wǎng)絡(luò)密集操作所需時(shí)間
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)測(cè)試多進(jìn)程并發(fā)執(zhí)行CPU密集操作所需時(shí)間
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)
Output: 54.342000007629395、53.437999963760376
(10)測(cè)試多進(jìn)程并發(fā)執(zhí)行IO密集型操作
t = time.time()
ios = []
t = time.time()
for x in range(10):
process = Process(target=io)
ios.append(process)
process.start()
e = ios.__len__()
while True:
for th in ios:
if not th.is_alive():
e -= 1
if e <= 0:
break
print("Multiprocess IO", time.time() - t)
Output: 12.509000062942505、13.059000015258789
(11)測(cè)試多進(jìn)程并發(fā)執(zhí)行Http請(qǐng)求密集型操作
t = time.time()
httprs = []
t = time.time()
for x in range(10):
process = Process(target=http_request)
ios.append(process)
process.start()
e = httprs.__len__()
while True:
for th in httprs:
if not th.is_alive():
e -= 1
if e <= 0:
break
print("Multiprocess Http Request", time.time() - t)
Output: 0.5329999923706055、0.4760000705718994
實(shí)驗(yàn)結(jié)果
| CPU密集型操作 | IO密集型操作 | 網(wǎng)絡(luò)請(qǐng)求密集型操作 | |
|---|---|---|---|
| 線性操作 | 94.91824996469 | 22.46199995279 | 7.3296000004 |
| 多線程操作 | 101.1700000762 | 24.8605000973 | 0.5053332647 |
| 多進(jìn)程操作 | 53.8899999857 | 12.7840000391 | 0.5045000315 |
通過(guò)上面的結(jié)果,我們可以看到:
多線程在IO密集型的操作下似乎也沒(méi)有很大的優(yōu)勢(shì)(也許IO操作的任務(wù)再繁重一些就能體現(xiàn)出優(yōu)勢(shì)),在CPU密集型的操作下明顯地比單線程線性執(zhí)行性能更差,但是對(duì)于網(wǎng)絡(luò)請(qǐng)求這種忙等阻塞線程的操作,多線程的優(yōu)勢(shì)便非常顯著了
多進(jìn)程無(wú)論是在CPU密集型還是IO密集型以及網(wǎng)絡(luò)請(qǐng)求密集型(經(jīng)常發(fā)生線程阻塞的操作)中,都能體現(xiàn)出性能的優(yōu)勢(shì)。不過(guò)在類似網(wǎng)絡(luò)請(qǐng)求密集型的操作上,與多線程相差無(wú)幾,但卻更占用CPU等資源,所以對(duì)于這種情況下,我們可以選擇多線程來(lái)執(zhí)行
以上所述是小編給大家介紹的Python單線程多線程和多進(jìn)程效率對(duì)比詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
PyTorch之nn.ReLU與F.ReLU的區(qū)別介紹
這篇文章主要介紹了PyTorch之nn.ReLU與F.ReLU的區(qū)別介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
Python爬蟲(chóng)+Tkinter制作一個(gè)翻譯軟件的示例
這篇文章主要介紹了Python爬蟲(chóng)+Tkinter制作一個(gè)翻譯軟件的示例,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-02-02
python中multiprosessing模塊的Pool類中的apply函數(shù)和apply_async函數(shù)的區(qū)別
這篇文章主要介紹了python中multiprosessing模塊的Pool類中的apply函數(shù)和apply_async函數(shù)的區(qū)別、文章圍繞主題的相關(guān)內(nèi)容展開(kāi)詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
Python用SSH連接到網(wǎng)絡(luò)設(shè)備
這篇文章主要介紹了Python用SSH連接到網(wǎng)絡(luò)設(shè)備,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-02-02
Python socket實(shí)現(xiàn)多對(duì)多全雙工通信的方法
今天小編就為大家分享一篇Python socket實(shí)現(xiàn)多對(duì)多全雙工通信的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Python實(shí)現(xiàn)計(jì)算AUC的示例代碼
AUC(Area?under?curve)是機(jī)器學(xué)習(xí)常用的二分類評(píng)測(cè)手段,直接含義是ROC曲線下的面積。本文將利用Python語(yǔ)言實(shí)現(xiàn)計(jì)算AUC,感興趣的可以學(xué)習(xí)一下2022-07-07

