title: Python中單線程、多線程與多進程的效率對比實驗
date: 2016-09-30 07:05:47
tags: [多線程,多進程,Python]
categories: [Python]html
meta: Python中多線程和多進程的對比
Python是運行在解釋器中的語言,查找資料知道,python中有一個全局鎖(GIL),在使用多進程(Thread)的狀況下,不能發揮多核的優點。而使用多進程(Multiprocess),則能夠發揮多核的優點真正地提升效率。
對比實驗
資料顯示,若是多線程的進程是CPU密集型的,那多線程並不能有多少效率上的提高,相反還可能會由於線程的頻繁切換,致使效率降低,推薦使用多進程;若是是IO密集型,多線程進程能夠利用IO阻塞等待時的空閒時間執行其餘線程,提高效率。因此咱們根據實驗對比不一樣場景的效率python
操做系統 |
CPU |
內存 |
硬盤 |
Windows 10 |
雙核 |
8GB |
機械硬盤 |
<!--more-->ios
(1)引入所須要的模塊
import requests
import time
from threading import Thread
from multiprocessing import Process
(2)定義CPU密集的計算函數
def count(x, y):
# 使程序完成50萬計算
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 |
99.96799993515015 |
IO密集 |
24.25 |
21.76699995994568 |
21.769999980926514 |
22.060999870300293 |
網絡請求密集型 |
4.519999980926514 |
8.563999891281128 |
4.371000051498413 |
14.671000003814697 |
(6)測試多線程併發執行CPU密集操做所需時間
counts = []
t = time.time()
for x in range(10):
thread = Thread(target=count, args=(1,1))
counts.append(thread)
thread.start()
while True:
e = len(counts)
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))
ios.append(thread)
thread.start()
while True:
e = len(ios)
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()
ios = []
t = time.time()
for x in range(10):
thread = Thread(target=http_request)
ios.append(thread)
thread.start()
while True:
e = len(ios)
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密集操做所需時間
counts = []
t = time.time()
for x in range(10):
process = Process(target=count, args=(1,1))
counts.append(process)
process.start()
while True:
e = len(counts)
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)
ios.append(process)
process.start()
while True:
e = len(ios)
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請求密集型操做
t = time.time()
httprs = []
t = time.time()
for x in range(10):
process = Process(target=http_request)
ios.append(process)
process.start()
while True:
e = len(httprs)
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|web
實驗結果
|
CPU密集型操做 |
IO密集型操做 |
網絡請求密集型操做 |
線性操做 |
94.91824996469 |
22.46199995279 |
7.3296000004 |
多線程操做 |
101.1700000762 |
24.8605000973 |
0.5053332647 |
多進程操做 |
53.8899999857 |
12.7840000391 |
0.5045000315 |
經過上面的結果,咱們能夠看到:網絡
- 多線程在IO密集型的操做下彷佛也沒有很大的優點(也許IO操做的任務再繁重一些就能體現出優點),在CPU密集型的操做下明顯地比單線程線性執行性能更差,可是對於網絡請求這種忙等阻塞線程的操做,多線程的優點便很是顯著了
- 多進程不管是在CPU密集型仍是IO密集型以及網絡請求密集型(常常發生線程阻塞的操做)中,都能體現出性能的優點。不過在相似網絡請求密集型的操做上,與多線程相差無幾,但卻更佔用CPU等資源,因此對於這種狀況下,咱們能夠選擇多線程來執行
![多線程的效果 多線程的效果](http://static.javashuo.com/static/loading.gif)
此文爲1年隨手寫的,多謝評論區指出謬誤,因數據是平均數,影響不大,故未作修改多線程