python 多线程与多进程的简单测试比较
来源:互联网 发布:带网络驱动的winpe 编辑:程序博客网 时间:2024/05/16 04:40
想必对接触过python的朋友都知道,python的多线程由于历史原因有一个GIL,并不是真正的多线程。所以查了些许资料亲自跑了下程序比较了下。
测试环境
macbook pro, 双核四线程,i5处理器。
比较对象
- 单线程
- 多线程
- 多进程
执行的任务都是耗cpu型的任务
单线程
# thread_test.pyimport randomimport threadingdef list_append(count, id, out_list): """ Creates an empty list and then appends a random number to the list 'count' number of times. A CPU-heavy operation! """ for i in range(count): out_list.append(random.random())def main(): size = 10000000 # Number of random numbers to add threads = 2 # Number of threads to create # Create a list of jobs and then iterate through # the number of threads appending each thread to # the job list # jobs = [] for i in range(0, threads): out_list = list() thread = threading.Thread(target=list_append(size, i, out_list)) thread.start() thread.join()if __name__ == "__main__": main()
单线程执行两次list_append函数。
多线程
# thread_test.pyimport randomimport threadingdef list_append(count, id, out_list): """ Creates an empty list and then appends a random number to the list 'count' number of times. A CPU-heavy operation! """ for i in range(count): out_list.append(random.random())def main(): size = 10000000 # Number of random numbers to add threads = 2 # Number of threads to create # Create a list of jobs and then iterate through # the number of threads appending each thread to # the job list jobs = [] for i in range(0, threads): out_list = list() thread = threading.Thread(target=list_append(size, i, out_list)) jobs.append(thread) # Start the threads (i.e. calculate the random number lists) for j in jobs: j.start() # Ensure all of the threads have finished for j in jobs: j.join()if __name__ == "__main__": main()
开双线程执行同一个函数。
多进程
# multiproc_test.pyimport randomimport multiprocessingdef list_append(count, id, out_list): """ Creates an empty list and then appends a random number to the list 'count' number of times. A CPU-heavy operation! """ for i in range(count): out_list.append(random.random())def main(): size = 10000000 # Number of random numbers to add procs = 2 # Number of processes to create # Create a list of jobs and then iterate through # the number of processes appending each process to # the job list jobs = [] for i in range(0, procs): out_list = list() process = multiprocessing.Process(target=list_append, args=(size, i, out_list)) jobs.append(process) # Start the processes (i.e. calculate the random number lists) for j in jobs: j.start() # Ensure all of the processes have finished for j in jobs: j.join()if __name__ == "__main__": main()
开双进程执行同一个函数。
测试结果
$ time python single_thread_test.py real 0m4.590suser 0m4.082ssys 0m0.448s
$ time python thread_test.py real 0m4.426suser 0m3.900ssys 0m0.456s
$ time python process_test.py real 0m2.436suser 0m4.028ssys 0m0.657s
测试程序用时用的是time指令,传送门在此可以自行学习。
可以从上面的测试结果看到,单线程与双线程在耗时方面并没有太多的区别,但是改为双进程后可以很明显的看到,耗时少了接近一半。这很好的说明了python多进程才是真正的并发。
当然python的多线程也不是一无是处,还是多线程有时相较于单线程还是有优势的,具体可以看这篇博客:传送门
参考文献:
https://www.quantstart.com/articles/parallelising-python-with-threading-and-multiprocessing
http://blog.csdn.net/thinkerabc/article/details/647272
http://www.redicecn.com/html/Python/20111223/355.html
http://python.jobbole.com/84205/
0 0
- python 多线程与多进程的简单测试比较
- python多进程与多线程比较
- 多进程与多线程的深度比较
- 多进程与多线程的深度比较
- 多进程与多线程的深度比较
- 多线程与多进程比较
- 多线程与多进程比较
- Python的多线程与多进程实践
- Python多线程多进程和线性执行的比较
- python多线程与多进程 超简单使用
- Python多线程与多进程
- Python多线程与多进程
- Linux多进程与多线程的深度比较
- 多进程与多线程的退出状态以及深度比较
- Linux多进程与多线程的深度比较
- 什么是多线程和与进程的比较
- TCP服务器多线程 多进程简单测试
- python 多线程,多进程执行测试
- 读写文件相关操作
- 编程珠玑第二章习题2.6(1-4)
- 数据结构 线段树 HDU 1754I Hate It(单点更新)
- 新技能get
- 监听器实现网站计数并获取用户信息
- python 多线程与多进程的简单测试比较
- 利用Spring的@Value注解读取配置文件
- shared_ptr
- 玩转Android之数据库框架greenDAO3.0使用指南
- idea部署tomcat启动成功后访问提示404
- Echarts3.0:实现一个页面放多个Echarts图表的方法
- 开机出现grub rescue,修复办法
- Leetcode 147. Insertion Sort List (Medium) (cpp)
- javaee之struts2框架学习