问题描述
我有以下代码,我想将任务分散到多进程中。经过实验,我意识到增加 cpu 内核的数量会对执行时间产生负面影响。
我的机器上有 8 个内核
-
案例 1:不使用多处理
-
- 执行时间:106 分钟
-
案例 2:多处理使用
ncores
= 4 -
- 执行时间:37 分钟
-
案例 3:multiprocessing 使用
ncores
= 7 -
- 执行时间:40 分钟
以下代码:
import time
import multiprocessing as mp
def _fun(i,args1=10):
#Sort matrix W
#For loop 1 on matrix M
#For loop 2 on matrix Y
return value
def run1(ncores=mp.cpu_count()):
ncores = ncores - 4 # use 4 and 1 to have ncores = 4 and 7
_f = functools.partial(_fun,args1=x)
with mp.Pool(ncores) as pool:
result = pool.map(_f,range(n))
return [t for t in result]
start = time.time()
list1= run1()
end = time.time()
print( 'time {0} minutes '.format((end - start)/60))
我的问题是,使用多处理的最佳实践是什么?据我了解,我们使用 cpu 内核的次数越多,速度就会越快。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)