2013-03-14 2 views
0

defaultdict (int)를 사용하여 일련의 책에서 단어 발생 수를 기록합니다.defaultdict (int)에 대한 MemoryError

File "C:\Python32\lib\collections.py", line 540, in update 
    _count_elements(self, iterable) 
MemoryError 

내 카운터의 크기가 8,000,000 이상 : 나는 메모리 예외를 얻을 때

파이썬은 램의 1.5 기가 소모된다.

나는 적어도 20,000,000 개의 고유 단어가 있습니다. 메모리 예외가 발생하지 않도록하려면 어떻게해야합니까?

+1

데이터 세트에 몇 개의 고유 단어가 있습니까? – NPE

+0

@ NPE 20,000,000 – Baz

+0

알기. 한 단어의 평균 길이는 얼마입니까? – NPE

답변

1

메모리가 많은 64 비트 시스템을 사용하는 경우에도 dict을 사용하여 추적하는 것이 타당한 생각이라고 생각하지 않습니다. 당신은 데이터베이스를 사용해야합니다. 만 포함 된 항목에 대한 공간을 제공하지 않는, 또한 새로운 아이템 슬롯 - code에서

/* If we added a key, we can safely resize. Otherwise just return! 
* If fill >= 2/3 size, adjust size. Normally, this doubles or 
* quaduples the size, but it's also possible for the dict to shrink 
* (if ma_fill is much larger than ma_used, meaning a lot of dict 
* keys have been * deleted). 
* 
* Quadrupling the size improves average dictionary sparseness 
* (reducing collisions) at the cost of some memory and iteration 
* speed (which loops over every possible entry). It also halves 
* the number of expensive resize operations in a growing dictionary. 
* 
* Very large dictionaries (over 50K items) use doubling instead. 
* This may help applications with severe memory constraints. 
*/ 
if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2)) 
    return 0; 
return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used); 

은, 당신이 너무 많은 항목을 삽입한다면, 딕셔너리 성장하는 것을 말한다. dict의 2/3 이상이 채워지면 dict의 크기는 두 배가됩니다 (또는 50,000 개 미만의 항목에서는 4 배가됩니다). 개인적으로 나는 수십만 개 미만의 품목을 담기 위해 사전을 사용합니다. 백만 개 미만의 항목으로도 8GB의 win7 시스템을 거의 동결시키지 않고 몇 기가 바이트 만 소모합니다. 당신이 할 수있는

단순히 항목을 계산하는 경우 : 합리적인 청크 크기

spilt the words in chunk 
count the words in each chunk 
update the database 

(병목이 될 것입니다 가정 데이터베이스 액세스) 일부 DB querys을 실행 훨씬 더 IMO 될 것입니다.