????Python??????????????е???????????????????python??????????????GIL?????????????(Thread)??????£?????????????????????????(Multiprocess)??????????????????????????Ч???
??????????
???????????????????????????CPU???????????????????ж???Ч???????????????????????????????л???????Ч???????????????????????IO?????????????????????IO???????????????????????????????Ч???????????????????????????Ч??
?????????? CPU ??? ??? Windows 10 ??? 8GB ??е??? (1)??????????????
????<code class="python">import requests
????import time
????from threading import Thread
????from multiprocessing import Process</code>
????(2)????CPU??????????
????<code class="python">def count(x?? y):
????# ????????150?????
????c = 0
????while c < 500000:
????c += 1
????x += x
????y += y</code>
??????3??????IO??????????д????
????<code class="python">def write():
????f = open("test.txt"?? "w")
????for x in range(5000000):
????f.write("testwrite ")
????f.close()
????def read():
????f = open("test.txt"?? "r")
????lines = f.readlines()
????f.close()</code>
????(4) ??????????????
????<code class="python">_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}</code>
????(5)???????????IO?????????CPU?????????????????????????????????????
????<code class="python"># 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)</code>
???????
????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??????????????
????<code class="python">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)</code>
????Output: 99.9240000248 ??101.26400017738342??102.32200002670288
????(7)??????????????IO??????????????
????<code class="Python">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)</code>
????Output: 25.69700002670288??24.02400016784668