Skip to main content
 首页 » 编程设计

python之为什么增加 worker 数量(多于核心数量)仍然会减少执行时间

2025年05月04日12Terrylee

我始终确信拥有比 CPU 内核更多的线程/进程没有意义(从性能角度来看)。但是,我的 python 示例显示了不同的结果。

import concurrent.futures 
import random 
import time 
 
 
def doSomething(task_num): 
    print("executing...", task_num) 
    time.sleep(1)  # simulate heavy operation that takes ~ 1 second     
    return random.randint(1, 10) * random.randint(1, 500)  # real operation, used random to avoid caches and so on... 
 
 
def main(): 
    # This part is not taken in consideration because I don't want to 
    # measure the worker creation time 
    executor = concurrent.futures.ProcessPoolExecutor(max_workers=60) 
 
    start_time = time.time() 
 
    for i in range(1, 100): # execute 100 tasks 
        executor.map(doSomething, [i, ]) 
    executor.shutdown(wait=True) 
 
    print("--- %s seconds ---" % (time.time() - start_time)) 
 
 
if __name__ == '__main__': 
    main() 

程序结果:

1 WORKER --- 100.28233647346497 seconds ---
2 WORKERS --- 50.26122164726257 seconds ---
3 WORKERS --- 33.32741022109985 seconds ---
4 WORKERS --- 25.399883031845093 seconds ---
5 WORKERS --- 20.434186220169067 seconds ---
10 WORKERS--- 10.903695344924927 seconds ---
50 WORKERS--- 6.363946914672852 seconds ---
60 WORKERS--- 4.819359302520752 seconds ---

只有 4 个逻辑处理器,这如何能更快地工作?

这是我的电脑规范(在 Windows 8 和 Ubuntu 14 上测试):

CPU Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz Sockets: 1 Cores: 2 Logical processors: 4

请您参考如下方法:

原因是因为 sleep() 只使用了微不足道的 CPU。在这种情况下,它不能很好地模拟线程执行的实际工作。

sleep() 真正做的是挂起线程,直到计时器到期。当线程挂起时,它不使用任何 CPU 周期。