超过核心的多线程性能 [英] Performance of multi-threading exceeding cores

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

问题描述

如果我有一个启动线程数量为 X 的进程,那么 X 会比 CPU 数量多的地方获得性能提升>核心(假设所有线程都在同步工作而无需异步调用存储/网络)?

If I have a process that starts X amount of threads, will there ever be a performance gain having X higher than the number of CPU cores (assuming all the threads are working synchronously without async calls to storage/network)?

EG如果我有两个内核 CPU ,是否会降低从3个以上持续工作的线程中启动应用程序的速度?

E.G. If I have a two cores CPU, will I just slow down the application starting 3+ constantly working threads?

推荐答案

这实际上取决于您的代码做什么。

It really depends on what your code does. it is too broad.

具有比内核更多的线程可能会加快程序的运行速度,例如,如果某些线程处于睡眠状态或试图阻塞锁。在这种情况下,OS调度程序可以唤醒不同的线程,并且该线程将在另一个线程处于睡眠状态时起作用。

Having more threads than cores might speed up the program for example if some of the threads sleep or try to block on a lock. in this case, the OS scheduler can wake different thread and that thread will work while the other thread is sleeping.

线程多于内核数也可能会减少程序执行时间,因为OS调度程序必须执行更多工作才能在线程执行之间进行切换,并且调度可能是一项繁重的操作。

Having more threads than the number of cores may also decrease the program execution time because the OS scheduler has to do more work to switch between the threads execution and that scheduling might be a heavy operation.

与往常一样,使用不同数量的线程对应用程序进行基准测试是实现最佳性能的最佳方法。还有一些算法(例如Hill-Climbing)可以帮助应用程序在运行时微调最佳线程数。

As always, benchmarking your application with different amount of threads is the best way to achieve maximum performance. there are also algorithms (like Hill-Climbing) which may help the application fine tune the best number of threads on runtime.

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

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