Python多处理计算明显慢于顺序计算

时间:2017-09-28 21:12:17

标签: python multiprocessing

我在python中玩多处理。我尝试将阵列上的计算分配到多个CPU核心。为了做到这一点,我提出了与multiprocessing.cpu_count()一样多的进程返回,并且我将数组的子集传递给进程(通过对数组索引进行分区)。该数组作为共享内存对象进行操作。 但是,对于不同的数组大小,我无法体验任何运行时改进。这是为什么? 这只是一个玩具示例,我并没有尝试通过这种计算来实现某些目标。

import multiprocessing as mp
import numpy as np
import time
import sharedmem


def some_function_mult(q, arr, index, width):
q.put((sum(arr[index:index+width])/np.amax(arr[index:index+width])**2)/40)

def some_function(arr, index, width):
return sum((arr[index:index+width])/np.amax(arr[index:index+width])**2)/40

def main(): 
num = mp.cpu_count()
n = 200000000
width  = n/num
random_array = np.random.randint(0,255,n)                                                                                                                                                                        
shared = sharedmem.empty(n)
shared[:] = random_array
print (shared)


queue = mp.Queue()

processes = [mp.Process(target=some_function_mult, args=(queue, shared, i*width, width)) for i in xrange(num)]

start_time = time.time()
for p in processes:
    p.start()

result = []
for p in processes:
    result.append(queue.get())

for p in processes:
    p.join()
end_time = time.time()

print ('Multiprocessing execution time = ' + str(end_time-start_time))
print (result)

result = []
start_time  =time.time()

for i in range(num):
    result.append(some_function(random_array, i*width, width))
end_time = time.time()

print ('Sequential processing time = '  + str(end_time-start_time))
print (result)

if __name__ == '__main__':
main()

0 个答案:

没有答案