Re: [問題] hashmap 的效能 (300mb檔案)
假設有 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
08/27 16:10, 1F
推
08/27 16:57, , 2F
08/27 16:57, 2F
推
08/27 20:06, , 3F
08/27 20:06, 3F
→
08/27 20:19, , 4F
08/27 20:19, 4F
→
08/27 20:23, , 5F
08/27 20:23, 5F
→
08/27 20:34, , 6F
08/27 20:34, 6F
推
08/30 10:25, , 7F
08/30 10:25, 7F
討論串 (同標題文章)
完整討論串 (本文為第 4 之 7 篇):