ProcessPoolExecutor 在 windows 上比在 mac 上花费更多时间

ProcessPoolExecutor takes more time on windows than on mac

from concurrent.futures import ProcessPoolExecutor
import time

class Foo():
    def __init__(self, name):
        self.name = name
        self.start = time.time()

    def log(self):
        for i in range(1000):
            time.sleep(0.001)
        print(f"{self.name} - Processing time: {(time.time() - self.start)}")

class Bar():
    def __init__(self, name):
        self.name = name
        self.start = time.time()

    def log(self):
        for i in range(1000):
            time.sleep(0.001)
        print(f"{self.name} - Processing time: {(time.time() - self.start)}")

class FooBar():
    def __init__(self, name):
        self.name = name
        self.start = time.time()

    def log(self):
        for i in range(1000):
            time.sleep(0.001)
        print(f"{self.name} - Processing time: {(time.time() - self.start)}")

def main():

    c1 = Foo("1")
    c2 = Foo("2")
    c3 = Bar("3")
    c4 = Bar("4")
    c5 = FooBar("5")
    c6 = FooBar("6")

    with ProcessPoolExecutor(max_workers=12) as executor:
        executor.submit(c1.log)
        executor.submit(c2.log)
        executor.submit(c3.log)
        executor.submit(c4.log)
        executor.submit(c5.log)
        executor.submit(c6.log)

if __name__ == "__main__":

    main()

Mac 在 ~1.18 秒内完成每个日志调用,windows 每次调用需要 ~15.71 秒。 Mac 有一个 6 核 2.6 GHz 处理器,Windows 有一个 6 核 2.4 GHz 处理器。

为什么 windows 执行同一个程序要慢将近 15 倍?

该问题与并发无关,而与每个操作系统设置的休眠解决方案有关。 Windows 的最小时间延迟为 15 毫秒,这归因于更长的等待时间。为了获得类似的性能,需要降低时间分辨率。

可在此处找到有关如何执行此操作的答案: