Python中單執行緒 多執行緒與多程序的效率對比實驗

2022-04-29 14:27:06 字數 4692 閱讀 4370

python是執行在直譯器中的語言,查詢資料知道,python中有乙個全域性鎖(gil),在使用多程序(thread)的情況下,不能發揮多核的優勢。而使用多程序(multiprocess),則可以發揮多核的優勢真正地提高效率。

對比實驗

資料顯示,如果多執行緒的程序是cpu密集型的,那多執行緒並不能有多少效率上的提公升,相反還可能會因為執行緒的頻繁切換,導致效率下降,推薦使用多程序;如果是io密集型,多執行緒程序可以利用io阻塞等待時的空閒時間執行其他執行緒,提公升效率。所以我們根據實驗對比不同場景的效率

作業系統

cpu記憶體

硬碟windows 10

雙核8gb

機械硬碟

(1)引入所需要的模組

import requests

import time

from threading import thread

from multiprocessing import process

def count(x, y):

# 使程式完成50萬計算

c = 0

while c < 500000:

c += 1

x += x

y += y

(2)定義cpu密集的計算函式

(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()

_head = 

except exception as e:

return

(4) 定義網路請求函式

(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)

# 網路請求密集型操作

(6)測試多執行緒併發執行cpu密集操作所需時間

輸出

counts = 

t = time.time()

for x in range(10):

thread = thread(target=count, args=(1,1))

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密集操作所需時間

def io():

write()

read()

t = time.time()

ios =

t = time.time()

for x in range(10):

thread = thread(target=count, args=(1,1))

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)測試多執行緒併發執行網路密集操作所需時間
t = time.time()

(9)測試多程序併發執行cpu密集操作所需時間

counts = 

t = time.time()

for x in range(10):

process = process(target=count, args=(1,1))

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)測試多程序併發執行io密集型操作
t = time.time()

ios =

t = time.time()

for x in range(10):

process = process(target=io)

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)測試多程序併發執行http請求密集型操作

實驗結果

cpu密集型操作

io密集型操作

網路請求密集型操作

線性操作

94.91824996469

22.46199995279

7.3296000004

多執行緒操作

101.1700000762

24.8605000973

0.5053332647

多程序操作

53.8899999857

12.7840000391

0.5045000315

通過上面的結果,我們可以看到:

>

單執行緒 多執行緒

1.基於python的單執行緒示例 from time import ctime,sleep import time def play video video for i in range 2 print i am playing video s at s video,ctime sleep 5 d...

一 單執行緒與多執行緒

一 多執行緒的例子 公司樓下有一家超市加做 永輝超市 下班了就去裡面買點菜,回去做做自己喜歡的飯菜。如果進去逛一圈,會發現它的收銀台是這樣的 它有好幾個收銀台,每個收銀台有乙個收銀員。付費的時候你可以選擇任意一條 我相信你肯定會選擇人最少的那一條 二 單執行緒的例子 除了這種大中型的超市,下班途中還...

單執行緒和多執行緒

what 1.程序 當乙個程式開始執行時,它就是乙個程序,程序包括執行中的程式和程式所使用到的記憶體和系統資源。2.執行緒 執行緒就是程式中的乙個執行流,每個執行緒都有自己的專有暫存器 棧指標 程式計數器等 但 是可以共享的,即不同的執行緒可以執行相同的函式。3.多執行緒 多執行緒是指程式中包含多個...