我有以下代码,它生成产生给定sum(n)的所有可能组合。但是,对于大数(n),此代码需要很长时间。有没有办法可以跨多个处理器并行化我的代码?
from itertools import combinations_with_replacement
def all_combination(numbers, n):
result = [seq for i in range(n, 0, -1) for seq in combinations_with_replacement(numbers,i) if sum(seq) == n]
return result
numbers = [1, 2, 3, 4, 5, 6]
n=700
print len(all_combination(numbers,n))
答案 0 :(得分:0)
from itertools import product
import math
import multiprocessing
def parallel_combination(i, limit):
numbers = [1, 2, 3, 4, 5, 6]
result=0
for seq in combinations_with_replacement(numbers, i):
if sum(seq) == limit:
result+=1
return result
def chunks(min_value, max_value):
for i in range(max_value, min_value, -1):
yield i
if __name__ == "__main__":
max_value=610
limit=610
min_value=int(math.floor(float(limit/6)))
pool = multiprocessing.Pool()
n_processesor=32
chunk_size=int((math.ceil(float((max_value-min_value))/n_processesor)))
processes = pool.map(func=parallel_combination, limit, iterable=chunks(min_value,max_value), chunksize=chunk_size)
final_result=0
for process in processes:
if process:
final_result+=process
print final_result