Python

时间:2017-04-06 15:54:39

标签: python parallel-processing multiprocessing

我正在尝试按照我在互联网上找到的说明在Python中并行化一个简单的for循环。

这是我的代码:

import time
from joblib import Parallel, delayed
import multiprocessing

n = 100000
k = 3
num_cores = multiprocessing.cpu_count()

def processInput(i):
    return i**2

#This should be the parallelized loop
if __name__ == '__main__':
    start = time.time()
    results = Parallel(n_jobs=num_cores)(delayed(processInput)(i) for i in range(n))
    print("Time parallelized loop:")
    print(time.time()-start)

运行此脚本所需的时间如下:

Time parallelized loop:
5.14900016785

非常奇怪的事实是,如果我运行顺序循环,时间会小得多。这是顺序循环:

import time
from joblib import Parallel, delayed
import multiprocessing

n = 100000
k = 3
num_cores = multiprocessing.cpu_count()

def processInput(i):
    return i**2

#This should be the squential loop
start = time.time()
results = [processInput(i) for i in range(n)]
print("Time Sequential loop:")
print(time.time() - start)

我得到这个时间:

Time Sequential loop:
0.107000112534

此时显然我遗漏了一些东西。顺序循环如何比并行循环快得多?

0 个答案:

没有答案