Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test program to show the performance characteristics of HashMap in Java.
package com.serendio.test;
import gnu.trove.map.hash.THashMap;
import gnu.trove.map.hash.TIntIntHashMap;
import java.util.HashMap;
import java.util.Map;
public class JavaMapTest {
private static final int LOOPSIZE = 1000000;
public static void main(String args[]) {
String ints[] = new String[LOOPSIZE];
for (int i = 0; i < LOOPSIZE; i++) {
ints[i] = Integer.toString(i);
}
long start = System.currentTimeMillis();
Map<String, Integer> map1 = new HashMap<String, Integer>();
for (int i = 0; i < LOOPSIZE; i++) {
map1.put(ints[i], new Integer(i));
}
long end = System.currentTimeMillis();
System.out.println("String hasing Put time:"
+ Long.toString(end - start));
start = System.currentTimeMillis();
for (int i = 0; i < LOOPSIZE; i++) {
String s = Integer.toString(i);
map1.get(s);
}
end = System.currentTimeMillis();
System.out.println("String hasing Get time:"
+ Long.toString(end - start));
start = System.currentTimeMillis();
Map<Integer, Integer> map2 = new HashMap<Integer, Integer>();
for (int i = 0; i < LOOPSIZE; i++) {
Integer entry = new Integer(i);
map2.put(entry, entry);
}
end = System.currentTimeMillis();
System.out.println("Integer hashing Put time:"
+ Long.toString(end - start));
start = System.currentTimeMillis();
for (int i = 0; i < LOOPSIZE; i++) {
Integer entry = new Integer(i);
map2.get(entry);
}
end = System.currentTimeMillis();
System.out.println("Integer hashing Get time:"
+ Long.toString(end - start));
start = System.currentTimeMillis();
THashMap<Integer, Integer> map3 = new THashMap<Integer, Integer>();
for (int i = 0; i < LOOPSIZE; i++) {
Integer entry = new Integer(i);
map3.put(entry, entry);
}
end = System.currentTimeMillis();
System.out.println("THashMap Integer hashing Put time:"
+ Long.toString(end - start));
start = System.currentTimeMillis();
for (int i = 0; i < LOOPSIZE; i++) {
Integer entry = new Integer(i);
map3.get(entry);
}
end = System.currentTimeMillis();
System.out.println("THashMap Integer hashing Get time:"
+ Long.toString(end - start));
start = System.currentTimeMillis();
TIntIntHashMap m4 = new TIntIntHashMap();
for (int i = 0; i < LOOPSIZE; i++) {
Integer entry = new Integer(i);
m4.put(entry, entry);
}
end = System.currentTimeMillis();
System.out.println("TIntIntHashMap Integer hashing Put time:"
+ Long.toString(end - start));
start = System.currentTimeMillis();
for (int i = 0; i < LOOPSIZE; i++) {
Integer entry = new Integer(i);
m4.get(entry);
m4.a
}
end = System.currentTimeMillis();
System.out.println("TIntIntHashMap Integer hashing Get time:"
+ Long.toString(end - start));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment