Python中单线程、多线程和多进程的效率对比实验

时间:2021-09-12 20:45:38

GIL机制导致如下结果:

Python的多线程程序并不能利用多核CPU的优势 (比如一个使用了多个线程的计算密集型程序只会在一个单CPU上面运行)
python多线程适合io操作密集型的任务(如socket server 网络并发这一类的)
python多线程不适合cpu密集操作型的任务,主要使用cpu来计算,如大量的数学计算。
那么如果有cpu密集型的任务怎么办,可以通过多进程来操作(不是多线程)。
假如CPU有8核,每核CPU都可以用1个进程,每个进程可以用1个线程来进行计算。

 1、线性模式测试

 import requests
import time
from threading import Thread
from multiprocessing import Process #定义CPU密集的计算函数
def count(x, y):
# 使程序完成150万计算
c = 0
while c < 500000:
c += 1
x += x
y += y #定义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() def io():
write()
read() #定义网络请求函数
_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} #---------------------------------------
#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):
io()
print("Line IO", time.time() - t) # 网络请求密集型操作
t = time.time()
for x in range(10):
http_request()
print("Line Http Request", time.time() - t)
 --运行---------------------结果:
('Line cpu', 97.26900005340576)
('Line IO', 24.319000005722046)
('Line Http Request', 209.94899988174438)

2、线程模式测试
 #定于线程公共函数
def mythread(fun,*args):
counts = []
for x in range(10):
thread = Thread(target=fun, args=args)
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 #测试多线程并发执行CPU密集操作所需时间
t = time.time()
mythread(count,1,1)
print("thread cpu ",time.time() - t) #测试多线程并发执行IO密集操作所需时间
t = time.time()
mythread(io)
print("thread IO ",time.time() - t) #测试多线程并发执行网络密集操作所需时间
t = time.time()
mythread(http_request)
print("Thread Http Request", time.time() - t)
--运行---------------------结果:

('thread cpu ', 102.20300006866455)
('thread IO ', 654.5730001926422)
('Thread Http Request', 21.170999765396118)

3.进程模式测试

 def myprocess(fun,*args):
counts = []
for x in range(10):
process = Process(target=fun,args=args)
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 if __name__ == '__main__': #没这句会报错。
#测试多进程并发执行CPU密集操作所需时间
t = time.time()
myprocess(count,1,1)
print("Multiprocess cpu", time.time() - t) #测试多进程并发执行IO密集型操作
t = time.time()
myprocess(io)
print("Multiprocess IO", time.time() - t) #测试多进程并发执行Http请求密集型操作
t = time.time()
myprocess(http_request)
print("Multiprocess Http Request", time.time() - t)
--运行---------------------结果:

('Multiprocess cpu', 20.168999910354614)
('Multiprocess IO', 11.82699990272522)
('Multiprocess Http Request', 21.805000066757202)

实验结果

  CPU密集型操作 IO密集型操作 网络请求密集型操作
单线程操作 97 24 310
多线程操作 102 654
多进程操作 22

通过上面的结果,我们可以看到:

  • 多线程在IO密集型的操作下似乎也没有很大的优势,在CPU密集型的操作下明显地比单线程线性执行性能更差,但是对于网络请求这种忙等阻塞线程的操作,多线程的优势便非常显著了
  • 多进程无论是在CPU密集型还是IO密集型以及网络请求密集型(经常发生线程阻塞的操作)中,都能体现出性能的优势。不过在类似网络请求密集型的操作上,与多线程相差无几,但却更占用CPU等资源,所以对于这种情况下,我们可以选择多线程来执行

一句话总结:cpu和io密集操作使用多进程,网络操作使用多线程