我需要创建并填充一个巨大的数组(例如96GB,72000行*72000列),其中每个元素都来自于数学公式。该数组将在计算后生成。
import itertools, operator, time, copy, os, sys
import numpy
from multiprocessing import Pool
def f2(x): # more complex mathematical formulas that change according to values in *i* and *x*
temp=[]
for i in combine:
temp.append(0.2*x[1]*i[1]/64.23)
return temp
def combinations_with_replacement_counts(n, r): #provide all combinations of r balls in n boxes
size = n + r - 1
for indices in itertools.combinations(range(size), n-1):
starts = [0] + [index+1 for index in indices]
stops = indices + (size,)
yield tuple(map(operator.sub, stops, starts))
global combine
combine = list(combinations_with_replacement_counts(3, 60)) #here putted 60 but need 350 instead
print len(combine)
if __name__ == '__main__':
t1=time.time()
pool = Pool() # start worker processes
results = [pool.apply_async(f2, (x,)) for x in combine]
roots = [r.get() for r in results]
print roots [0:3]
pool.close()
pool.join()
print time.time()-t1
- 如何快速创建和填充大型numpy数组?先填充列表,然后聚合再转换成numpy数组是最快的方法吗?
- 如果2d数组的行、列和案例之间相互独立,可以并行计算以加速数组的填充吗?使用多进程优化此类计算的线索/路径有哪些?