当前位置:网站首页>Can multithreading optimize program performance?
Can multithreading optimize program performance?
2022-07-28 09:47:00 【biyusr】
ask : If a monk carries water , Two monks carry water to drink , Three monks have no water to drink , So must the flame be high when people collect firewood ?
Is multithreading sure to improve program performance ? In computer science , The standard answer to this question is “it depends”, Look at the situation .
All the designs in the computer are to make more efficient use of hardware resources , Include :CPU、 Memory 、IO etc. , Let's have a look at the same .
Multithreading and CPU
Multithreading and CPU Programmers know the most , We know that one of the purposes of multi-wire is to make full use of multi-core , But there is a premise here that the task you want to deal with can really be divided into independent subtasks .

for instance , If you want to sum all the elements of an array , Then this task can be split into two independent subtasks : Mission A Calculate the sum of the first half of the array elements , Mission B Calculate the sum of the last half of the array elements , Then the task A And tasks B Leave it to two threads to execute .
If it is in a multi-core system, this kind of multi-threaded parallel processing will significantly improve program performance , But there is an upper limit to the performance improvement brought by using multithreading to make full use of multi-core .
It's simple , It's better than building a house , Building a house is a modest project , It's not impossible to let one person finish it , But another six or seven people can obviously speed up the project , But if hundreds of workers build a house again, the speed may slow down , After all, resources are limited ( Available tools, etc ), More people need more time to coordinate , The same is true of multithreading , When the number of threads exceeds a certain critical point, the operating system starts to get busy ( Frequent scheduling switching ), I call it the phenomenon of three monks without water .

But if the system is single core , Then this task splitting will not have any effect , Because no matter how many threads you create really work CPU only one .
Of course, it's also possible that we can't split the task at all , Problems like calculating Fibonacci series , If you can't calculate f(n-1) And f(n-2) Solution , Then you have no way to calculate the current problem f(n) Solution , The two tasks that are split A and B There's a back and forth dependency , At this time, multithreading is useless .
There's another possibility , Your problem is very small , If this array has hundreds of thousands of elements , Then it doesn't make much sense for you to use multithreading , At this time, the benefits brought by using multithreading are not enough to offset the performance overhead brought by multithreading .
Multithreading and IO
Multithreading can certainly improve the performance of programs IO Performance ? The answer is obviously not .
The simplest scenario is like this , Your program needs to read and write data from a very slow Internet link , In this case, one thread is probably enough to cope , Creating multiple threads may be detrimental to program performance .
The same will happen on disk , A thread may have filled the disk , At this time, creating multiple threads to read and write files obviously cannot speed up the processing speed of the program .

And on the server side , Programmers also use multithreading to speed up program processing , ad locum , A typical problem is blocking network IO It will cause the calling thread to be suspended and suspended , At this point, the easiest way is to create multiple threads , Each thread processes a request , But as the number of requests increases, more and more threads will be created , At this time, the phenomenon of three monks without water began to appear ,IO Multiplexing technology can solve this problem well .
Of course , If your scene is IO Will block the processing thread , Then create two threads at this time , One is responsible for processing data , One is responsible for waiting IO, Then this will obviously improve program performance .
Multithreading and memory
Memory is actually the same as disk , There is also a read-write bandwidth limit , But our programs generally do not reach the upper limit of memory read and write bandwidth , This is not a bottleneck .
The bottleneck lies in the memory resources shared by multiple threads ( data ) And multi-core systems cache Consistency issues .
Generally speaking , For multi-threaded shared resources, mutually exclusive access is usually required , However, to speed up memory reading and writing , Modern processors have cache System (L1、L2、L3), Each core has its own cache, these cache Will cache memory data , In other words, a piece of data may exist in memory and all cores at the same time cache in , This will bring classic data consistency problems : Some core has been modified cache The data in the needs to be synchronized to other cores , This requires that cache There must be a consistent protocol in the system , Otherwise, the program may read the wrong ( overdue ) data .

However, this kind of synchronization has performance loss , Multiple threads frequently operate on the same variable, which may cause the processor cache The system needs to synchronize frequently among various cores , In extreme cases, the performance of multithreaded programs is even worse than that of single threads .
Therefore, do not share data between multiple threads without sharing , If you must share , Then try to control it in the minimum range , The frequency of reading and writing should be controlled to a minimum .
边栏推荐
- 【日志】日志干什么的?日志工厂是什么?log4j 的配置和使用? log4j.properties 文件配置、log4j jar包坐标
- opencv安装配置测试
- Window source code analysis (IV): window deletion mechanism
- 3分钟带你了解微信小程序开发
- 就这么一个简单的校验,80%的程序员却做不到,更不理解!
- Oracle creates users with query permission only
- 多线程一定能优化程序性能吗?
- 数据库核心体系
- [Guangxi University] information sharing of postgraduate entrance examination and re examination
- Changes in the relationship between data and application in IT industry
猜你喜欢

树上启发式合并

Introduction to shardingsphere's concept of sub database and sub table (2)

MATLAB的实时编辑器

JS array is de duplicated, the ID is the same, and a value is added and merged

How promise instance solves hell callback

Heuristic merging on tree

多线程一定能优化程序性能吗?

《PyTorch深度学习实践》第九课多分类问题(手写数字MNIST)
![[Download] several tools for brute force cracking and dictionary generation are recommended](/img/c6/f4a9c566ff21a8e133a8a991108201.png)
[Download] several tools for brute force cracking and dictionary generation are recommended
![[vscode] vscode usage](/img/0d/d6edbad047ecd7a092d4d7aa06e04d.png)
[vscode] vscode usage
随机推荐
[vscode] vscode usage
SQL Server、MySQL主从搭建,EF Core读写分离代码实现
C# 之 方法参数传递机制
The secret behind three salary increases a year
Express builds a simple local background (1)
[Guangxi University] information sharing of postgraduate entrance examination and re examination
Analysis of HashSet internal principle
CakePHP 4.4.3 release, PHP rapid development framework
IT行业数据与应用关系的变迁
[multithreading] non atomic agreement of long and double
实验五:用户与用户组管理
ARouter源码解析(三)
LeetCode - 哈希表专题
3分钟带你了解微信小程序开发
C# 倒计时工具
Window source code analysis (IV): window deletion mechanism
Window源码解析(四):Window的删除机制
MQ learning
Method parameter transfer mechanism of C #
数据库核心体系