


Comparative experiment
The data shows that if the multi-threaded process is CPU-intensive, multi-threading will not improve the efficiency much. On the contrary, the efficiency may decrease due to frequent switching of threads. It is recommended to use multi-threading; If it is IO-intensive, the multi-threaded process can use the idle time while waiting for IO blocking to execute other threads to improve efficiency. So we compare the efficiency of different scenarios based on experiments
(1) Introduce the required modules
import requests import time from threading import Thread from multiprocessing import Process
(2) Define CPU-intensive computing functions
def count(x, y): # 使程序完成150万计算 c = 0 while c < 500000: c += 1 x += x y += y
(3) Define IO-intensive file reading and writing functions
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) Define the network request function
_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) Test the time required for linear execution of IO-intensive operations, CPU-intensive operations, and time required for network request-intensive operations
# 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)
Output
CPU-intensive: 95.6059999466, 91.57099986076355 92.52800011634827, 99.96799993515015
IO intensive: 24.25, 21.76699995994568, 21.769999980926514, 22.060999870300293
Network request intensive: 4.519999980926514, 8 .563999891281128, 4.371000051498413, 4.522000074386597, 14.671000003814697
(6) Test the time required for multi-threaded concurrent execution of CPU-intensive operations
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)
Output: 99.9240000248, 101.26400017738342, 102.32200002670288
(7) Test the time required for multi-threaded concurrent execution of IO-intensive operations
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)
Output: 25.69700002670288, 24.024000167846 68
(8) Test the time required for multi-threaded concurrent execution of network-intensive operations
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)
Output: 0.7419998645782471, 0.3839998245239258, 0.3900001049041748
(9) Test the time required for multiple processes to concurrently perform CPU-intensive operations
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)
Output: 54.342000007629395, 53.437999 963760376
(10) Test the concurrent execution of IO-intensive operations by multiple processes
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)
Output: 12.509000062942505, 13.059000015258789
(11) Test multi-process concurrent execution of Http request-intensive operations
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)
Output: 0.5329999923706055, 0.4760000705718994
Experimental results
Through the above results, we can see:
Multiple threads are IO intensive There doesn't seem to be a big advantage under the type of operation (perhaps the advantage will be reflected if the IO operation task is heavier). Under the CPU-intensive operation, the performance is obviously worse than the single-threaded linear execution, but for network requests, For operations that block threads such as busy waiting, the advantages of multi-threading are very obvious
Multiple processes can show performance advantages in CPU-intensive, IO-intensive, and network request-intensive operations (thread-blocking operations often occur). However, for network request-intensive operations, it is almost the same as multi-threading, but it takes up more resources such as CPU, so in this case, we can choose multi-threading to execute

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
