实际上,多线程应用程序会比单线程应用程序快吗? [英] Will a multi-threaded application be actually faster than a single-threaded application?

查看:362
本文介绍了实际上,多线程应用程序会比单线程应用程序快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部都是理论上的,这个问题才浮现在脑海,我不确定答案到底是什么

All is entirely theoretical, the question just came to mind and I wasn't entirely sure whats the answer:

假设您有一个可以计算4个独立计算的应用程序. (完全独立,执行它们的顺序无关紧要,也不需要一个即可计算另一个). 还要假设这些计算很长(几分钟)并且受CPU限制(不等待任何类型的IO)

Assume you have an application that calculates 4 independent calculations. (Totally independent, doesn't matter what order you do them and you don't need one to calculate another). Also assume those calculations are long (minutes) and CPU-bound (not waiting for any kind of IO)

1)现在,如果您有一台 1-处理器计算机,则从逻辑上讲,单线程应用程序将比多线程应用程序快(或与多线程应用程序相同).由于计算机无法一次使用一个处理器来完成一件事情,因此它将浪费时间进行上下文切换等. 到目前为止一切还好吗?

1) Now, if you have a 1-processor computer, a single thread application will logically be faster than (or the same as) a multithreaded application. As the computer not able to do more then one thing at a time with one processor, it would "waste" time on context switching and the likes. So far so good?

2)如果您有一台 4处理器计算机,则最有可能有4个线程比单线程快.正确的?您的计算机现在一次可以执行4次操作,因此将您的应用程序划分为4个线程是合乎逻辑的,并且它应该以4个计算中最长的时间完成. 到目前为止还好吗?

2) If you have a 4 processor computer, 4 threads will mostly likely be faster for this than single thread. Right? your computer can now do 4 operations at a time so its just logical to divide your application to 4 threads, and it should complete with the time the longest of the 4 calculations take. Still good so far?

3)现在让我感到困惑的是,我为什么真正让我的应用程序创建的线程多于可用处理器(实际上是内核)的数量?我已经编程并看到了创建数十个和数百个线程的应用程序,但是实际上-完美的数量对于一台普通计算机来说大约是8个?

3) And now the actual part I am confused about - why would I EVER have my application create more threads than the number of processors (well actually - cores) available? I have programmed and have seen applications that create tens and hundreds of threads, but actually - the perfect number is about 8 for an average computer?

P.S.我已经读过这篇文章:线程与单线程 但是并没有安静地回答.

P.S. I already read this: Threading vs single thread but didn't quiet answer that.

欢呼

推荐答案

为什么我的应用程序创建的线程永远超过可用处理器(实际上是内核)的数量?

Why would I EVER have my application create more threads than the number of processors (well actually - cores) available?

一个很好的理由是,如果您有等待事件的线程.例如,您可能有一个生产者/消费者应用程序,其中的生产者正在从某个数据流中读取数据,并且这些数据以突发方式到达:成批地记录几百(或千)条记录,一段时间内没有记录,然后又有另一记录爆裂.假设您有一台四核计算机.您可能只有一个生产者线程来读取数据并将其放入队列中,而有三个消费者线程来处理队列.

One very good reason is if you have threads that wait on events. For example you might have a producer/consumer application in which the producer is reading from some data stream, and that data arrives in bursts: a few hundred (or thousand) records in a batch, followed by nothing for a while, and then another burst. Say you have a 4-core machine. You could have a single producer thread that reads the data and places it in a queue, and three consumer threads to process the queue.

或者,您可以有一个生产者线程和四个消费者线程.在大多数情况下,生产者线程处于空闲状态,从而为您提供了四个消费者线程来处理队列中的项目.但是,当项目在数据流中可用时,消费者线程之一就被换出,而有利于生产者.

Or, you could have a single producer thread and four consumer threads. Most of the time, the producer thread is idle, giving you four consumer threads to process items from the queue. But when items are available on the data stream, one of the consumer threads gets swapped out in favor of the producer.

这是一个简化的示例,但与我在生产中使用的程序基本相似.

That's a simplified example, but substantially similar to programs that I have in production.

更一般地说,创建比您具有处理单元(通常是CPU核心,尽管超线程的存在会稍微增加一些麻烦)的更多连续工作(即与CPU绑定)的线程没有任何意义.如果您知道自己的线程不会等待外部事件,那么只有n个内核时拥有n+1个线程将导致线程上下文切换浪费时间.请注意,这严格是在程序的上下文中.如果还有其他应用程序和OS服务正在运行,则应用程序的线程将不时换出,以便那些其他应用程序和服务可以获取时间片.但是有人认为,如果您正在运行CPU密集型程序,那么您将限制同时运行的其他应用程序和服务.

More generally, it doesn't make any sense to create more continuously-working (i.e. CPU bound) threads than you have processing units (CPU cores in general, although the existence of hyperthreading muddies the waters a bit). If you know that your threads won't be waiting on external events, then having n+1 threads when you only have n cores will end up wasting time with thread context switches. Note that this is strictly in the context of your program. If there are other applications and OS services running, your application's threads will get swapped out from time to time so that those other apps and services can get a timeslice. But one assumes that, if you're running a CPU-intensive program, you'll limit the other apps and services that are running at the same time.

当然,您最好的选择是进行测试.在四核计算机上,使用1、2、3、4、5,...线程测试您的应用程序.完成不同数量的线程花费的时间.我想您会发现在4核计算机上,最佳点是3或4;除非其他应用程序或OS服务占用大量CPU,否则很有可能是4.

Your best bet, of course, is to set up a test. On a 4-core machine, test your app with 1, 2, 3, 4, 5, ... threads. Time how long it takes to complete with different numbers of threads. I think you'll find that on a 4-core machine the sweet spot will be 3 or 4; most likely 4 unless there are other apps or OS services that take a lot of CPU.

这篇关于实际上,多线程应用程序会比单线程应用程序快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆