问题描述
几年前,在Windows环境下,我做了一些测试,通过让CPU计算密集型+内存访问密集型+I/O访问密集型应用程序运行多个实例.我开发了两个版本:一个在多处理下运行,另一个在多线程下运行.
我发现多处理的性能要好得多.我在别处读过(但我不记得那个网站了).
其中说明原因是在多线程下,他们在争夺"单个内存管道和I/O管道,这使得性能比多处理更差>
但是,我再也找不到那篇文章了.我想知道,直到今天,以下是否仍然适用?
在Windows中,有算法代码在多处理下运行,有很高的演出的机会比多线程更好.
这取决于各种线程或进程(我将使用统称任务"来表示它们)需要进行多少通信,尤其是通过共享内存:对于线程来说这很容易、便宜且快速,但对于进程来说根本不是,所以,如果很多事情正在发生,我敢打赌进程的性能不会击败线程'.
此外,进程(尤其是在 Windows 上)启动起来更繁重",因此如果发生大量任务启动",线程在性能方面很容易再次击败进程.
接下来,您可以拥有具有超线程"功能的 CPU,它可以(至少)非常快速地在一个内核上运行两个线程——但不是进程(因为超线程"线程不能使用不同的地址空间)——线程可以在性能方面获胜的另一种情况.
如果这些考虑都不适用,那么无论如何比赛应该不会比平局好.
A few years ago, in the Windows environment, I did some testing, by letting multiple instances of CPU computation intensive + memory access intensive + I/O access intensive application run. I developed 2 versions: One is running under multi-processing, another is running under multi-threading.
I found that the performance is much better for multi-processing. I read somewhere else (but I can't remember the site).
Which states that the reason is that under multi-threading, they are "fighting" for a single memory pipeline and I/O pipeline, which makes the performance worse compared to multi-processing
However, I can't find that article anymore. I was wondering, till today, whether the below still hold true?
It depends on how much the various threads or processes (I'll be using the collective term "tasks" for both of them) need to communicate, especially by sharing memory: that's easy, cheap and fast for threads, but not at all for processes, so, if a lot of it is going on, I bet processes' performance is not going to beat threads'.
Also, processes (esp. on Windows) are "heavier" to get started, so if a lot of "task starts" occur, again threads can easily beat processes in terms of performance.
Next, you can have CPUs with "hyperthreading", which can run (at least) two threads on a core very rapidly -- but, not processes (since the "hyperthreaded" threads cannot be using distinct address spaces) -- yet another case in which threads can win performance-wise.
If none of these considerations apply, then the race should be no better than a tie, anyway.
这篇关于多线程和多进程的性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!