【发布时间】:2013-07-11 05:16:59
【问题描述】:
我编写了一个递归函数,可以详尽地生成具有某些特征的矩阵。 函数是这样的:
def heavies(rowSums,colSums,colIndex,matH):
if colIndex == len(colSums) - 1:
for stuff in heavy_col_permutations(rowSums,colSums,colIndex):
matH[:,colIndex] = stuff[0]
yield matH.copy()
return
for stuff in heavy_col_permutations(rowSums,colSums,colIndex):
matH[:,colIndex] = stuff[0]
rowSums = stuff[1]
for matrix in heavies(rowSums,colSums,colIndex+1,matH):
yield matrix
heavy_col_permutations 是一个函数,它只返回具有我需要的特征的矩阵列。
问题在于,由于重量级会产生大量矩阵,因此会占用太多内存。 我最终从另一个函数一个接一个地调用它,最终我占用了太多的 RAM,我的进程被杀死了(我在有内存上限的服务器上运行它)。我怎样才能写这个以减少它使用的内存?
程序看起来像:
r = int(argv[1])
n = int(argv[2])
m = numpy.zeros((r,r),numpy.dtype=int32)
for row,col in heavy_listing(r,n):
for matrix in heavies(row,col,0,m):
# do more stuff with matrix
而且我知道函数很重是发生大量内存消耗的地方,我只需要减少它。
【问题讨论】: