c++ std::async :与 8 核相比,4 核更快

c++ std::async : faster on 4 cores compared to 8 cores

我有 16000 个作业要执行。

每项工作都是独立的。没有共享内存,没有进程间通信,没有锁或互斥锁。

我在 ubuntu 16.06。 C++11。英特尔® 酷睿™ i7-8550U CPU @ 1.80GHz × 8

我使用 std::async 在核心之间拆分作业。

如果我将作业分成 8 个(每个核心 2000 个),计算时间为 145。 如果我将作业分成 4 个(每个核心 4000 个),计算时间为 60。

两种情况下 reduce 后的输出相同。

如果我在计算过程中监控 CPU(仅使用 htop),事情会按预期发生(第一种情况下 8 个内核使用 100%,第二种情况下仅 4 个内核使用 100%)。

我很困惑为什么 4 核的处理速度比 8 核快得多。

i7-8550U有4核8线程

有什么区别?引用 How-To Geek:

Hyper-threading was Intel’s first attempt to bring parallel computation to consumer PCs. It debuted on desktop CPUs with the Pentium 4 HT back in 2002. The Pentium 4’s of the day featured just a single CPU core, so it could really only perform one task at a time—even if it was able to switch between tasks quickly enough that it seemed like multitasking. Hyper-threading attempted to make up for that.

A single physical CPU core with hyper-threading appears as two logical CPUs to an operating system. The CPU is still a single CPU, so it’s a little bit of a cheat. While the operating system sees two CPUs for each core, the actual CPU hardware only has a single set of execution resources for each core. The CPU pretends it has more cores than it does, and it uses its own logic to speed up program execution. In other words, the operating system is tricked into seeing two CPUs for each actual CPU core.

Hyper-threading allows the two logical CPU cores to share physical execution resources. This can speed things up somewhat—if one virtual CPU is stalled and waiting, the other virtual CPU can borrow its execution resources. Hyper-threading can help speed your system up, but it’s nowhere near as good as having actual additional cores.

将作业分配给比可用内核更多的内核 - 您将付出很大的代价。