我想知道是什么改变了我在编写代码时应该知道什么。为什么我的HashMap实现比JDK慢10倍?
- 使用相同的参数和方法
put()
,get()
而不打印 - 用于
System.NanoTime()
测试运行时 - 我具有1-10 INT键试图将其与10个值测试 时,所以每一个单个散列返回独特的索引,这是最优化的场景
- 基于此的HashSet实现几乎与JDK的一样快速
这里是我的简单的实现:
public MyHashMap(int s) {
this.TABLE_SIZE=s;
table = new HashEntry[s];
}
class HashEntry {
int key;
String value;
public HashEntry(int k, String v) {
this.key=k;
this.value=v;
}
public int getKey() {
return key;
}
}
int TABLE_SIZE;
HashEntry[] table;
public void put(int key, String value) {
int hash = key % TABLE_SIZE;
while(table[hash] != null && table[hash].getKey() != key)
hash = (hash +1) % TABLE_SIZE;
table[hash] = new HashEntry(key, value);
}
public String get(int key) {
int hash = key % TABLE_SIZE;
while(table[hash] != null && table[hash].key != key)
hash = (hash+1) % TABLE_SIZE;
if(table[hash] == null)
return null;
else
return table[hash].value;
}
这里的风向标:
public static void main(String[] args) {
long start = System.nanoTime();
MyHashMap map = new MyHashMap(11);
map.put(1,"A");
map.put(2,"B");
map.put(3,"C");
map.put(4,"D");
map.put(5,"E");
map.put(6,"F");
map.put(7,"G");
map.put(8,"H");
map.put(9,"I");
map.put(10,"J");
map.get(1);
map.get(2);
map.get(3);
map.get(4);
map.get(5);
map.get(6);
map.get(7);
map.get(8);
map.get(9);
map.get(10);
long end = System.nanoTime();
System.out.println(end-start+" ns");
}
如果没有测试显示比较的另一端,即使用'HashMap',问题是不完整的。你还应该展示你的微基准,因为这些微小的错误是完全错误的结果。 – dasblinkenlight