連想配列(HashMap)と配列の速度比較
HashMap<Integer, Integer>でgetメソッドでvalueを得るのと,
int[ ]の配列にして配列の要素を指定して値を得るのとどっちが早いのだろう.
ということで実験.
// Mapに値を入れていく
for (int i = 0; i < 10000000; i++) {
map.put(i, i);
hoge[i] = i;
}
System.out.println("速度実験開始");
// 速度実験
long start = System.currentTimeMillis();
int value = map.get(0);
long end = System.currentTimeMillis();
System.out.println("MAPのfirstは" + (end - start) + "ms");
start = System.currentTimeMillis();
value = map.get(9999999);
end = System.currentTimeMillis();
System.out.println("MAPのlastは" + (end - start) + "ms");
start = System.currentTimeMillis();
value = hoge[0];
end = System.currentTimeMillis();
System.out.println("配列のfirstは" + (end - start) + "ms");
start = System.currentTimeMillis();
value = hoge[9999999];
end = System.currentTimeMillis();
System.out.println("配列のlastは" + (end - start) + "ms");
}
結果としては...
どれも同じ.
(厳密には違うのかもしれないけど)
あんまり気にすることないのかなあ