Python中并行嵌套循环的性能不佳

时间:2017-12-14 19:06:41

标签: python python-multiprocessing

我尝试使代码更快,所以我希望循环并行运行。我的第一个问题是,如何在Python中实现并行循环以及在哪个循环中进行并行化?

这是我的代码:

from joblib import Parallel, delayed
import multiprocessing as mp
import numpy as np
import time

def compR(R, M, I, l):
    for p in range(ppl):
        for i in range(N):
            R[l][p] += np.dot(M[l][p],I[i])
    return R

level = 20
ppl = 8
x = 70
y = 10
N = 15

M = [[np.random.rand(x,y) for p in range(ppl)] for l in range(level)]
R = [[np.zeros((x,1)) for p in range(ppl)] for l in range(level)]
I = [np.random.rand(y,1) for n in range(N)]

# Seriell
t0 = time.time()
for l in range(level):
    for p in range(ppl):
        for i in range(N):
            R[l][p] += np.dot(M[l][p],I[i])
t1 = time.time()
print(t1-t0)

R = [[np.zeros((x,1)) for p in range(ppl)] for l in range(level)]

num_cores = mp.cpu_count()

# Parallel            
t0 = time.time()
R = Parallel(n_jobs=num_cores)(delayed(compR)(R, M, I, l) for l in range(level))            
t1 = time.time()
print(t1-t0)

实施是否正确?我试图让level循环并行运行,因为通常level> N> ppl。当我为更大数量的levelx以及y运行代码时,并行循环的性能非常糟糕。我做错了什么?

0 个答案:

没有答案