以下效果很好,但我想讓它更快。實際的應用程序可以處理Tuple1和Tuple2,每個元素有30,000個元素和17個嵌套序列。我看到很多關於更快循環的問題,並且嘗試了一個沒有改進的數組和映射。優化循環。更快的ResultList.append([c,d,c [1]/d [1]])?陣列?地圖?
for i in Tuple1:
print i
((1, 2.2, 3), (2, 3.3, 4))
((5, 6.6, 7), (6, 7.7, 8))
for i in Tuple2:
print i
((10, 11, 12), (11, 12, 13), (12, 13, 14))
((20, 21, 22), (21, 22, 23), (22, 23, 24))
ResultList = []
for a in Tuple1:
for b in Tuple2:
for c in a:
for d in b:
ResultList.append([c, d, c[1]/d[1]])
SomeFunction() # Processes ResultList and is not a concern.
ResultList=[]
由SomeFunction處理的ResultList的示例。
[(1, 2.2, 3), (10, 11, 12), 0.2]
[(1, 2.2, 3), (11, 12, 13), 0.18333333333333335]
[(1, 2.2, 3), (12, 13, 14), 0.16923076923076924]
[(2, 3.3, 4), (10, 11, 12), 0.3]
[(2, 3.3, 4), (11, 12, 13), 0.27499999999999997]
[(2, 3.3, 4), (12, 13, 14), 0.25384615384615383]
'from multiprocessing import Pool'然後使用'pool.map'在整個進程池中拆分外層循環。 – Duncan
我收到「ImportError:無法導入名稱池」。假設我編碼很差,我嘗試了一個只有「import multiprocessing print multiprocessing .__ file__」的腳本,並收到相同的錯誤。 – user3180110
好消息/壞消息。好消息是我使用了多處理池,並且使用Python的速度更快。壞消息是我使用PyPy運行實際的應用程序,使用PyPy的多處理池比沒有多處理池的PyPy慢。這對我來說毫無意義,所以我必須進一步研究它。 – user3180110