2017-03-07 74 views
0
file_dict = {} 
    for calli, callj in itertools.product(feats,feats): 
     keys = seqd.keys() 
     if (not calli in keys) | ((not callj in keys)): 
      continue 
     else: 
      lst = [] 
      ##### this is the problematic part !!!!!!!!! 
      for jj, ii in itertools.product(seqd[callj], seqd[calli]): 
       if (jj - ii) > 0: 
        lst.append(1./(jj - ii)) 
        del jj,ii 
      entry = sum(lst) 
      del lst 
      file_dict[str(calli) + " " + str(callj) + " distance"] = entry 

我在一些代碼中使用上面的代碼並遍歷文件。我有某種內存泄漏。如果我只是註釋掉我在循環中突出顯示的4行,我的代碼保持在常量RAM〜100mb。不過,當我取消這個評論時,它的速度將達到8-9GB。請幫忙!!內存泄漏 - 蟒蛇 - 計數器 - 在詞典中的列表

+0

你應該嘗試寫這樣的[MCVE],因爲它是,儘管笛卡爾產品應該讓你暫停,但其他人很難推斷。目前還不清楚你想要完成的是什麼,這些「del」的意義是什麼等等。「我的代碼被破壞了,修復它」往往是一個難以獲得幫助的問題。 – pvg

+0

注意:''|''是按位或運算符,而不是您想要的布爾運算符 - 在Python中拼寫爲''或''。 ''或''效率更高,因爲如果左側是真的,它可以跳過右側的評估(從而使結果無條件成立)。 – jasonharper

+0

感謝您的反饋!我找到了一個解決方案,並在下面發佈它! –

回答

0

對於上下文,我複製本文的第5頁上的算法,生成系統調用依賴關係圖。

這是完整的代碼修復。它基本上涉及在一個總和中使用列表理解,但沒有在理解上加上括號。這樣的項目進行了歸納,因爲他們產生的,而不是先建立列表...

下面是代碼:

def graph_maker_dict(feats, calls): 
# get dictionary of present calls and list of indexes where they appear 
seqd = defaultdict(list) 
for v, k in enumerate(calls): 
    seqd[k].append(v) 

# run calculations with list comprehensions 
file_dict = {} 
for calli, callj in itertools.product(feats,feats): 
    keys = seqd.keys() 
    if (not calli in keys) or ((not callj in keys)): 
     continue 
    else: 
     entry = sum(1./(jj - ii) for jj in seqd[callj] for ii in seqd[calli] if (jj - ii) > 0) 
     file_dict[calli[:2] + " " + callj[:2] + " distance"[:2]] = entry 
return file_dict 
+0

這就是所謂的'發電機表達',如果你需要谷歌它。 – pvg