摘要:中单线程多线程与多进程的效率对比实验多线程多进程中多线程和多进程的对比是运行在解释器中的语言,查找资料知道,中有一个全局锁,在使用多进程的情况下,不能发挥多核的优势。
title: Python中单线程、多线程与多进程的效率对比实验
date: 2016-09-30 07:05:47
tags: [多线程,多进程,Python]
categories: [Python]
Python是运行在解释器中的语言,查找资料知道,python中有一个全局锁(GIL),在使用多进程(Thread)的情况下,不能发挥多核的优势。而使用多进程(Multiprocess),则可以发挥多核的优势真正地提高效率。
资料显示,如果多线程的进程是CPU密集型的,那多线程并不能有多少效率上的提升,相反还可能会因为线程的频繁切换,导致效率下降,推荐使用多进程;如果是IO密集型,多线程进程可以利用IO阻塞等待时的空闲时间执行其他线程,提升效率。所以我们根据实验对比不同场景的效率
操作系统 | CPU | 内存 | 硬盘 |
---|---|---|---|
Windows 10 | 双核 | 8GB | 机械硬盘 |
import requests import time from threading import Thread from multiprocessing import Process
def count(x, y): # 使程序完成50万计算 c = 0 while c < 500000: c += 1 x += x y += y
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()
_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): 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 |
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 |
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 |
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 |
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 |
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 |
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|
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等资源,所以对于这种情况下,我们可以选择多线程来执行
此文为1年随手写的,多谢评论区指出谬误,因数据是平均数,影响不大,故未做修改
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/44266.html
摘要:批评的人通常都会说的多线程编程太困难了,众所周知的全局解释器锁,或称使得多个线程的代码无法同时运行。多线程起步首先让我们来创建一个名为的模块。多进程可能比多线程更易使用,但需要消耗更大的内存。 批评 Python 的人通常都会说 Python 的多线程编程太困难了,众所周知的全局解释器锁(Global Interpreter Lock,或称 GIL)使得多个线程的 Python 代码无...
摘要:使用进行并发编程篇三掘金这是使用进行并发编程系列的最后一篇。所以我考虑启用一个本地使用进行并发编程篇二掘金我们今天继续深入学习。 使用 Python 进行并发编程 - asyncio 篇 (三) - 掘金 这是「使用Python进行并发编程」系列的最后一篇。我特意地把它安排在了16年最后一天。 重新实验上篇的效率对比的实现 在第一篇我们曾经对比并发执行的效率,但是请求的是httpb...
摘要:协程,又称微线程,纤程。最大的优势就是协程极高的执行效率。生产者产出第条数据返回更新值更新消费者正在调用第条数据查看当前进行的线程函数中有,返回值为生成器库实现协程通过提供了对协程的基本支持,但是不完全。 协程,又称微线程,纤程。英文名Coroutine协程看上去也是子程序,但执行过程中,在子程序内部可中断,然后转而执行别的子程序,在适当的时候再返回来接着执行。 最大的优势就是协程极高...
摘要:多进程执行任务结束,创建进程和销毁进程是时间的,如果长度不够,会造成多线程快过多进程多线程执行任务结束,进程间通信生产者消费者模型与队列演示了生产者和消费者的场景。 进程 Python是运行在解释器中的语言,查找资料知道,python中有一个全局锁(GIL),在使用多进程(Thread)的情况下,不能发挥多核的优势。而使用多进程(Multiprocess),则可以发挥多核的优势真正地提...
摘要:一般用进程池维护,的设为数量。多线程爬虫多线程版本可以在单进程下进行异步采集,但线程间的切换开销也会随着线程数的增大而增大。异步协程爬虫引入了异步协程语法。 Welcome to the D-age 对于网络上的公开数据,理论上只要由服务端发送到前端都可以由爬虫获取到。但是Data-age时代的到来,数据是新的黄金,毫不夸张的说,数据是未来的一切。基于统计学数学模型的各种人工智能的出现...
阅读 3797·2021-11-24 09:39
阅读 1935·2021-11-16 11:45
阅读 654·2021-11-16 11:45
阅读 1109·2021-10-11 10:58
阅读 2535·2021-09-09 11:51
阅读 1984·2019-08-30 15:54
阅读 745·2019-08-29 13:13
阅读 3505·2019-08-26 12:18