在Python 3.X中搜索各种可能性以进行多处理后,非常简单的程序。
在MAC和Windows上运行程序会提供不同的结果。与串行相比,MAC并行处理花费的时间更少。我阅读了有关IPC开销等的各种说明,因为这是一个很容易处理的功能,但是为什么在MAC OSX上结果更好。
基本上知道处理Pool.MAP()的MAC OSX架构的人可以提供输入。提前谢谢。
**On MAC OSX**
cpu_count() = 4
Creating pool with 4 processes
pool = <multiprocessing.pool.Pool object at 0x1019d96a0>
Time for y_parallel = 2.866562843322754
Time for y_serial = 10.207499027252197
**On Windows**
cpu_count() = 4
Creating pool with 4 processes
pool = <multiprocessing.pool.Pool object at 0x031A1CD0>
Time for y_parallel = 13.155309915542603
Time for y_serial = 10.025831460952759
import multiprocessing
import time
def f(x):
time.sleep(0.1)
return x+1
# Serially adding 100 numbers (X+1) to y_serial
y_serial = []
x = range(100)
startTime_serial = time.time()
#for i in x:
# y_serial = y_serial + [f(i)]
y_serial += map(f,x)
endTime_serial = time.time()
#Letting Pool allocate processes through map.Pool
if __name__ == '__main__':
print('cpu_count() = %d' % multiprocessing.cpu_count())
PROCESSES = multiprocessing.cpu_count()
print('Creating pool with %d processes' % PROCESSES)
pool = multiprocessing.Pool()
print('pool = %s' % pool)
startTime_parallel = time.time()
y_parallel = pool.map(f,x)
pool.close()
pool.join()
endTime_parallel = time.time()
print("Time for y_parallel = ", endTime_parallel - startTime_parallel)
print ("Time for y_serial = ", endTime_serial - startTime_serial)
print ("y_parallel", y_parallel)
print ("y_serial=", y_serial)