비교 실험
데이터에 따르면 멀티 스레드 프로세스가 CPU 집약적이라면 멀티 스레드는 효율성을 크게 향상시키지 못하고 오히려 스레드를 자주 전환하여 효율성이 저하될 수 있습니다. . IO 집약적인 경우 멀티 프로세스를 사용하는 것이 좋습니다. 멀티 스레드 프로세스는 IO 차단이 다른 스레드를 실행하는 동안 유휴 시간을 사용하여 효율성을 높일 수 있습니다. 그래서 실험을 바탕으로 다양한 시나리오의 효율성을 비교합니다.
|하드 디스크 | -|- ---|---------|
| Windows 10 | 듀얼 코어|8GB|기계식 하드 드라이브|
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, 99.96799993515015IO 집약적: 24.25, 21.766999959 94568, 21.769999980926 514, 22.060999870300293네트워크 요청 집중: 4.519999980926514, 8.563999891281128, 4.371000051498413, 4.5 22000074386597, 14.671000003 814697(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)출력: 99.9240000248, 101.26400017738342, 102.32200002 670288(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() e = ios.__len__() while True: for th in ios: if not th.is_alive(): e -= 1 if e <= 0: break print(time.time() - t)출력: 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() 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)출력: 0.7419998645782471, 0.38 39998245239258, 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() 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)출력: 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() 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)출력: 12.509000062942505, 13.05 9000015258789(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.5329999923 706055, 0.4760000705718994
실험 결과