對比實驗
資料顯示,如果多執行緒的進程是CPU密集型的,那多執行緒並不能有多少效率上的提升,相反還可能會因為執行緒的頻繁切換,導致效率下降,推薦使用多進程;如果是IO密集型,多執行緒程序可以利用IO阻塞等待時的空閒時間執行其他執行緒,提升效率。所以我們根據實驗比較不同場景的效率
| 作業系統| CPU | 記憶體| 硬碟|
|-----------|-------|------| --------|
| Windows 10 | 雙核心|8GB|機械硬碟|
(1)引進所需的模組
import requests import time from threading import Thread from multiprocessing import Process
(2)定義CPU密集的運算函數
def count(x, y): # 使程序完成150万计算 c = 0 while c < 500000: c += 1 x += x y += y
(3)定義IO密集的文件讀寫函數
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) 定義網路請求函數
_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密集操作所需時間、網路請求密集型操作所需時間
# 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、IO 99.96799993515015
密0926514、22.060999870300293
網路請求密集型: 14697
(6)測試多執行緒並發執行CPU密集操作所需時間
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.32200002670288rr
(7)測試多加執行緒並發執行操作密集時間88、24.02400016784668
(8)測試多執行緒並發執行網路密集操作所需時間
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: 0.7419998645782471、0.3839998245239258、0.3900001049041748
rr(9393001049041748
非常07629395、53.437999963760376(10)測試多進程並發執行IO密集型操作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: 12.509000062942505、13.059000015258789(11)測試多進程並發執行Http請求密集型操作
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)(11)測試多進程並發執行Http請求密集型操作
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)5718994
實驗結果
透過上面的結果,我們可以看到:
多執行緒在IO密集型的操作下似乎也沒有很大的優勢(也許IO操作的任務再繁重一些就能體現出優勢),在CPU密集型的操作下明顯地比單線程線性執行效能更差,但是對於網路請求這種忙等阻塞執行緒的操作,多執行緒的優勢便非常顯著了
多進程無論是在CPU密集型還是IO密集型以及網路請求密集型(經常發生執行緒阻塞的操作)中,都能體現出效能的優勢。不過在類似網路請求密集的操作上,與多執行緒相差無幾,但卻更佔用CPU等資源,所以對於這種情況下,我們可以選擇多執行緒來執行

Python的靈活性體現在多範式支持和動態類型系統,易用性則源於語法簡潔和豐富的標準庫。 1.靈活性:支持面向對象、函數式和過程式編程,動態類型系統提高開發效率。 2.易用性:語法接近自然語言,標準庫涵蓋廣泛功能,簡化開發過程。

Python因其簡潔與強大而備受青睞,適用於從初學者到高級開發者的各種需求。其多功能性體現在:1)易學易用,語法簡單;2)豐富的庫和框架,如NumPy、Pandas等;3)跨平台支持,可在多種操作系統上運行;4)適合腳本和自動化任務,提升工作效率。

可以,在每天花費兩個小時的時間內學會Python。 1.制定合理的學習計劃,2.選擇合適的學習資源,3.通過實踐鞏固所學知識,這些步驟能幫助你在短時間內掌握Python。

Python適合快速開發和數據處理,而C 適合高性能和底層控制。 1)Python易用,語法簡潔,適用於數據科學和Web開發。 2)C 性能高,控制精確,常用於遊戲和系統編程。

學習Python所需時間因人而異,主要受之前的編程經驗、學習動機、學習資源和方法及學習節奏的影響。設定現實的學習目標並通過實踐項目學習效果最佳。

Python在自動化、腳本編寫和任務管理中表現出色。 1)自動化:通過標準庫如os、shutil實現文件備份。 2)腳本編寫:使用psutil庫監控系統資源。 3)任務管理:利用schedule庫調度任務。 Python的易用性和豐富庫支持使其在這些領域中成為首選工具。

要在有限的時間內最大化學習Python的效率,可以使用Python的datetime、time和schedule模塊。 1.datetime模塊用於記錄和規劃學習時間。 2.time模塊幫助設置學習和休息時間。 3.schedule模塊自動化安排每週學習任務。

Python在遊戲和GUI開發中表現出色。 1)遊戲開發使用Pygame,提供繪圖、音頻等功能,適合創建2D遊戲。 2)GUI開發可選擇Tkinter或PyQt,Tkinter簡單易用,PyQt功能豐富,適合專業開發。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

SublimeText3 Linux新版
SublimeText3 Linux最新版

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器