简体   繁体   English

多处理映射所需的时间比串行处理的时间长

[英]Multiprocessing Map takes longer than serial

I have 2 sockets with each 20 cores so I would like to speed up some processes. 我有2个插槽,每个20核,所以我想加快一些进程。 But multiprocess are always slower than the serial "approach". 但是多进程总是比串行“方法”慢。 Is there a reason for that? 有什么理由吗? Am I not doing this the most efficient way? 我不是最有效的方法吗? Is it due to a lack of communication (pipe or queue) between processes? 是由于进程之间缺乏通信(管道或队列)吗?

import time
from multiprocessing import Pool
import numpy as np

#Classical approach by serial
startime = time.time()
def f(x):
    return np.sqrt(x)
f(np.arange(1000))
print("---%s seconds ---" % (time.time() - startime))

#Multiprocess test
startime = time.time()
if __name__ == '__main__':
    p = Pool(40)
    test = p.map(np.sqrt,np.arange(1000),chunksize=1)
print("---%s seconds ---" % (time.time() - startime))

---- EDIT --- ----编辑-

With parallel i need 2.92 sec and in serial i need less 1 sec... 并行我需要2.92秒,而串行我需要更少的1秒...

Starting processes is slow, even on a modern OS. 即使在现代OS上,启动过程也很慢。 Calculating 1000 square roots is blazing fast on modern hardware. 1000个计算平方根是在现代硬件上速度极快。

To reap benefits of parallel processing, you have to spend much more time on actual computation than on starting things up. 要获得并行处理的好处,您必须花更多的时间在实际计算上,而不是花在启动上。 Try computing something more expensive, like 1000 bcrypt s, or slow, like hitting 1000 different URLs. 尝试计算更昂贵的东西(如1000 bcrypt )或较慢的东西(如访问1000个不同的URL)。

With compute-intensive tasks, where every process eats 100% CPU, there's no point to have more processes than CPU cores. 对于计算密集型任务,每个进程都消耗100%的CPU,因此没有比CPU核心更多的进程的意义了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多处理要么永远不会完成,要么比串行处理花费更长的时间 - Multiprocessing either never finishes or takes longer than serial processing Python多处理/线程比虚拟机上的单个处理花费更长的时间 - Python multiprocessing/threading takes longer than single processing on a virtual machine Python:多处理地图需要更长的时间才能完成最后几个流程 - Python: Multiprocessing Map takes longer to complete last few processes 我的多处理线程池比单线程实现需要更长的时间来完成任务 - My multiprocessing threadpool takes longer to complete tasks than a single-threaded implementation 为什么使用 networkx 时 dask.delayed 比串行代码花费更长的时间? - Why dask.delayed takes longer than serial code when working with networkx? 列表长于进程数时的 Python multiprocessing.Pool.map 行为 - Python multiprocessing.Pool.map behavior when list is longer than number of processes 多处理比单(正常)处理花费更长的时间 - Multiprocessing taking longer than single (normal) processing 多处理执行比串行执行慢 - Multiprocessing execution slower than serial execution 为什么此多处理代码比串行代码慢? - Why this multiprocessing code is slower than the serial one? 多处理比Windows中的串行处理慢(但不是在Linux中) - Multiprocessing slower than serial processing in Windows (but not in Linux)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM