Re: [問題] hashmap 的效能 (300mb檔案)

看板java作者 (Gavin)時間11年前 (2012/08/27 15:13), 編輯推噓3(304)
留言7則, 4人參與, 最新討論串4/7 (看更多)
假設有 a.txt 以及 b.txt 兩個檔案 a.txt b.txt c1 c2 c3 c1 c2 c3 c4 將a.txt 轉成 map int first; while((line = br.readLine()) != null) { first = line.indexOf(" "); map.put(line.substring(first + 1).hashCode(), // 只存 hashcode line.substring(0, first)); } 不存 c2, c3 字串,可省下 2/3 記憶體 假設取代 b.txt 中 c4 int last; while((line = br.readLine()) != null) { first = line.indexOf(" "); last = line.lastIndexOf(" "); bw.write(line.substring(0, last) + " " //c1 c2 c3 + map.get(line.substring(first + 1, last).hashCode()) //c4 + "\n"); } 實測約30秒內跑完,看電腦狀況 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.123.85.140

08/27 16:10, , 1F
如果不同 string hashCode 一定不同的話可行
08/27 16:10, 1F

08/27 16:57, , 2F
[亂入] 用 Scanner.hasNext() 會不會更省咧?
08/27 16:57, 2F

08/27 20:06, , 3F
基本上面對這樣的資料量...選用資料庫解法會簡單省事很多 XD
08/27 20:06, 3F

08/27 20:19, , 4F
少了一千萬對 key value, 改用一千萬次 db query :x
08/27 20:19, 4F

08/27 20:23, , 5F
join column就好了? :$
08/27 20:23, 5F

08/27 20:34, , 6F
5F 突破盲點了!
08/27 20:34, 6F

08/30 10:25, , 7F
謝謝!!! 從來沒有想過要用 hashCode 的方式 QQ
08/30 10:25, 7F
文章代碼(AID): #1GEnu39U (java)
討論串 (同標題文章)
文章代碼(AID): #1GEnu39U (java)