ProcessPoolExecutor在Windows上比在Mac上花费更多的时间

问题描述 投票:0回答:1
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倍?

execution-time concurrent.futures
1个回答
0
投票

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

有关此操作的答案可在此处找到:https://stackoverflow.com/a/43505033/4431136

© www.soinside.com 2019 - 2024. All rights reserved.