java map对象的效率比较
来源:互联网 发布:数组转字符串 编辑:程序博客网 时间:2024/06/10 08:45
@SuppressWarnings("unchecked")
public class MapPerformance {
public static void main(String[] args) {
MapPerformance test = new MapPerformance(10 * 10000);
out.print(StringUtils.center("Test Map Performance: loop=" + test.loop, 80, '-'));
out.printf("\n sssssss", "", "put", "put", "remove", "get",
"iterator","for");
test.benchmark(new HashMap());
test.benchmark(new Hashtable());
test.benchmark(new LinkedHashMap());
test.benchmark(new IdentityHashMap());
test.benchmark(new TreeMap());
test.benchmark(new WeakHashMap());
test.benchmark(new ConcurrentHashMap());
}
private int loop = 10000;
public MapPerformance(int loop) {
this.loop = loop;
}
public void benchmark(Map map) {
out.printf("\n s", map.getClass().getSimpleName());
int j;
StopWatch watch = null;
//1.测试顺序性能(Add)
(watch = new StopWatch()).start();
for (int i = 0; i < loop; i++) {
map.put(i, i);
}
watch.stop();
out.printf("d", watch.getTime());
//2.测试随机插入性能(Random insert)
public class MapPerformance {