为什么 Google Compute Engine 的计算优化 CPU 在 Python 中处理数字比笔记本电脑慢

Why is Google Compute Engine's compute-optimized CPU slower than a laptop on number crunching in Python

我在我的 Precision 5520 笔记本电脑和 Google Compute Engine 的 c2-standard-4 上都是 运行 以下代码:

import multiprocessing as mp
import time

def foo():
    before = time.time()
    sum = 0
    for i in range(1, 100000000):
        sum += i
    print(time.time() - before, sum)

for i in range(mp.cpu_count()):
    mp.Process(target=foo).start()

在这两种情况下,mp.cpu_count() 都是 4。然而,出乎我意料的是,笔记本电脑上的每次计算需要 5.2 秒,而 GCE 需要 8.6 秒。

笔记本电脑运行 Xeon E3-1505M v6 @ 3GHz。这是一个强大的 CPU,但我希望笔记本电脑 CPU 无法与 Google HPC 服务器上的 CPU 相提并论(例如,由于热量限制)。

Python 笔记本电脑上的版本是 3.8.5。 GCE 有 Python 版本 3.9.2。

为什么会这样?

Update 根据@John Hanley 的回复,我更改了 Google Cloud Platform 上的配置,因此每个内核仅使用一个 vCPU现在时间是 4 秒而不是 8.6 秒。这太惊人了,因为它似乎破坏了使用超线程的整个想法...

在您的笔记本电脑上,每个 CPU 核心是两个 Hyper-Threads。

在云中,一个虚拟化CPU (vCPU) 是一个Hyper-Thread。

你的笔记本电脑,在相同的主板、时钟速度、内存等条件下,每 CPU 的等效 CPU 功率是其两倍。当您在云中配置 VCPU 时,您正在配置物理 CPU 核心的 one-half。

On Compute Engine, each virtual CPU (vCPU) is implemented as a single hardware multithread on one of the available CPU processors. On Intel Xeon processors, Intel Hyper-Threading Technology supports multiple app threads running on each physical processor core. You configure your Compute Engine VM instances with one or more of these multithreads as vCPUs. The specific size and shape of your VM instance determines the number of its vCPUs.

Google Cloud CPU Platforms