ORIGINAL: 我编写的代码表面上是并行的,但实际上并没有更快地运行 - 并行和非并行版本需要相同的时间。对于我的生活,我无法弄清楚为什么。
我在Windows 7上通过Anaconda使用Python 3.4。无论是在IDE(Spyder)还是命令提示符中提交作业,结果都是一样的。这是我的代码:
import multiprocessing
from multiprocessing import Pool
import time
def divide_by_two(n):
time.sleep(.1)
return n/2
if __name__ == '__main__':
print("The number of cores is ", multiprocessing.cpu_count())
pool = Pool(processes=multiprocessing.cpu_count())
smallList = range(0,1000) #[0, 2, 4, 6, 8, 10]
print('Checking the parallelized way', smallList[:5])
start = time.time()
result = pool.map(divide_by_two, smallList)
end = time.time()
cleaned = [x for x in result if not x is None]
print('small List divided by two is ', str(cleaned[:5]))
print('Parallel way takes ', str(end-start), ' seconds')
#Now the dumb version
print('Checking the slow way', smallList[:5])
start2 = time.time()
smallList = range(0,1000) #[0, 2, 4, 6, 8, 10]
result2 = map(divide_by_two, smallList)
end2 = time.time()
cleaned2 = [x for x in result2 if not x is None]
print('small List divided by two is ', str(cleaned2[:5]))
print('The slow way takes', str(end2-start2), ' seconds')
这是输出:
The number of cores is 4
Checking the parallelized way range(0, 5)
small List divided by two is [0.0, 0.5, 1.0, 1.5, 2.0]
Parallel way takes 26.87681818008423 seconds
Checking the slow way range(0, 5)
small List divided by two is [0.0, 0.5, 1.0, 1.5, 2.0]
The slow way takes 0.0 seconds
答案 0 :(得分:0)
感谢评论者。如果可以的话,会投票评论。增加了并行化问题的大小,这次使用了正确的变量(oops!)并使用了不同的计时功能。这是:
import multiprocessing
from multiprocessing import Pool
import time
def divide_by_two(n):
time.sleep(.1)
return n/2
if __name__ == '__main__':
print("The number of cores is ", multiprocessing.cpu_count())
pool = Pool(processes=multiprocessing.cpu_count())
smallList = range(0,1000) #[0, 2, 4, 6, 8, 10]
print('Checking the parallelized way', smallList[:5])
start = time.clock() #time.time()
print('Start 1 is ', start)
result = pool.map(divide_by_two, smallList)
end = time.time()
cleaned = [x for x in result if not x is None]
print('small List divided by two is ', str(cleaned[:5]))
print('Parallel way takes ', time.clock()-start, ' seconds')
#Now the dumb version
print('Checking the slow way', smallList[:5])
start = time.clock()
print('Start 2 is ', start)
result2 = map(divide_by_two, smallList)
cleaned2 = [x for x in result2 if not x is None]
print('small List divided by two is ', str(cleaned2[:5]))
print('The slow way takes', time.clock()-start, ' seconds')