Pythonでのシングルスレッド、マルチスレッド、マルチプロセスの効率の比較実験

Pythonはインタープリターで実行される言語です。情報を調べると、Pythonにはグローバルロック(GIL)があることがわかります。マルチプロセス(スレッド)を使用する場合、マルチコアを利用できません。マルチプロセス(マルチプロセス)を使用すると、マルチコアを利用して効率を大幅に向上させることができます。

比較実験

データによると、マルチスレッドプロセスがCPUを集中的使用する場合、マルチスレッドは効率をあまり向上させません。逆に、スレッドの切り替えが頻繁に行われるため、効率が低下する可能性があります。IOを集中的に使用する場合は、マルチプロセスをお勧めします。、マルチスレッドプロセスは、IOブロッキングのアイドル時間を使用して、他のスレッドの実行を待機して効率を向上させることができます。そこで、実験に基づいてさまざまなシナリオの効率を比較します

オペレーティング・システム CPU ハードディスク
ウィンドウズ10 デュアルコア 8GB メカニカルハードディスク
(1)必要なモジュールをインポートします
       
        
        
1
2
3
4
       
        
        
インポート リクエスト
インポート 時間
スレッドからインポートスレッド
マルチプロセッシングインポートプロセスから
(2)CPUを集中的に使用する計算関数を定義する
       
        
        
1
2
3
4
5
6
7
       
        
        
def count (x、y)
#プログラムに500,000回の計算を完了させる
c = 0
しばらく 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密集操作所需时间
       
        
        
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密集操作所需时间
       
        
        
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)测试多线程并发执行网络密集操作所需时间
       
        
        
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密集操作所需时间
       
        
        
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)

Output: 54.342000007629395、53.437999963760376

(10)测试多进程并发执行IO密集型操作
       
        
        
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):
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("マルチプロセスIO"、time.time()-t)

出力: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()

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)

出力:0.5329999923706055、0.4760000705718994


実験結果
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://blog.atomicer.cn/2016/09/30/Python%E4%B8%AD%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%92%8C% E5%A4%9A%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%AF%B9%E6%AF%94 /

おすすめ

転載: blog.csdn.net/mrliqifeng/article/details/78466512