每个核心的最佳线程数 [英] Optimal number of threads per core

查看:152
本文介绍了每个核心的最佳线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个4核CPU,并且我想在最短的时间内运行某些进程.理想情况下,该过程是可并行化的,因此我可以在无限数量的线程上运行它的块,并且每个线程花费相同的时间.

Let's say I have a 4-core CPU, and I want to run some process in the minimum amount of time. The process is ideally parallelizable, so I can run chunks of it on an infinite number of threads and each thread takes the same amount of time.

由于我有4个内核,因此我不希望通过运行比内核更多的线程来提高速度,因为单个内核只能在给定的时刻运行单个线程.我对硬件不是很了解,所以这只是一个猜测.

Since I have 4 cores, I don't expect any speedup by running more threads than cores, since a single core is only capable of running a single thread at a given moment. I don't know much about hardware, so this is only a guess.

在比内核更多的线程上运行可并行化进程是否有好处?换句话说,如果我使用4000个线程而不是4个线程运行它,那么我的过程会更快,更慢还是在相同的时间内完成?

推荐答案

如果您的线程不执行I/O,同步等操作,并且没有其他任何运行,则每个内核1个线程将为您带来最佳性能.但是,很有可能不是这种情况.添加更多的线程通常会有所帮助,但是在某些时候,它们会导致性能下降.

If your threads don't do I/O, synchronization, etc., and there's nothing else running, 1 thread per core will get you the best performance. However that very likely not the case. Adding more threads usually helps, but after some point, they cause some performance degradation.

不久前,我正在相当不错的负载下在Mono上运行ASP.NET应用程序的2个四核计算机上进行性能测试.我们使用了最小和最大线程数,最后我们发现,对于在特定配置中的特定应用程序,最佳吞吐量在36到40个线程之间.在这些界限之外的任何事情都表现得更糟.学过的知识?如果我是您,我将使用不同数量的线程进行测试,直到找到适合您应用程序的数量为止.

Not long ago, I was doing performance testing on a 2 quad-core machine running an ASP.NET application on Mono under a pretty decent load. We played with the minimum and maximum number of threads and in the end we found out that for that particular application in that particular configuration the best throughput was somewhere between 36 and 40 threads. Anything outside those boundaries performed worse. Lesson learned? If I were you, I would test with different number of threads until you find the right number for your application.

肯定的一件事:4k线程将花费更长的时间.有很多上下文切换.

One thing for sure: 4k threads will take longer. That's a lot of context switches.

这篇关于每个核心的最佳线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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