[英]Python - Parallelism not working
編輯:我在評論中得到了很好的反饋。 對代碼進行了一些更改,但仍然遇到類似的問題。 現在看來我的計時方式有問題,因為第二次操作要花費超過0秒的時間。
原文:我寫的代碼表面上看起來是並行的,但實際上運行起來並不快-並行和非並行版本都花費相同的時間。 對於我的一生,我不知道為什么。
我正在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
感謝評論者。 如果可以的話,我會投票贊成。 並行化問題的大小增加,這次使用了正確的變量(糟糕!),並且使用了不同的計時功能。 這里是:
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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.