[英]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.