java map对象的效率比较
2011-12-19 20:19
337 查看
@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)
(watch = new StopWatch()).start();
for (int i = 0; i < loop; i++) {
j = (int) (Math.random() * loop);
map.put(j, new Integer(-j));
}
watch.stop();
out.printf("d", watch.getTime());
//3.测试随机索引删除(Random remove)
(watch = new StopWatch()).start();
for (int i = 0; i < loop; i++) {
j = (int) (Math.random() * loop);
map.remove(j);
}
watch.stop();
out.printf("d", watch.getTime());
//4.测试随机取数性能(Random get)
(watch = new StopWatch()).start();
for (int i = 0; i < loop; i++) {
j = (int) (Math.random() * loop);
map.get(j);
}
watch.stop();
out.printf("d", watch.getTime());
//6.测试迭代性能(Iterator)
(watch = new StopWatch()).start();
Iterator<Object> iter = map.values().iterator();
while (iter.hasNext()) {
iter.next();
}
watch.stop();
out.printf("d", watch.getTime());
//7.测试迭代性能(Iterator)
(watch = new StopWatch()).start();
// Iterator<Object> iter = list.iterator();
for (Object obj : map.values()) {
}
watch.stop();
out.printf("d", watch.getTime());
}
}
结果:
-----------------------Test Map Performance: loop=100000------------------------
put random-put remove get
iterator for
HashMap 47 94 78
47 0 0
Hashtable 141 78 78 62
0 16
LinkedHashMap 125 78 94 47
0 0
IdentityHashMap 172 265 47 63 15
16
TreeMap 94 125 187 109
0 0
WeakHashMap 156 250 47 31
0 16
ConcurrentHashMap 94 140 79 46 0
16
结论:
1、HashMap的性能比较高,但不是线程安全的
2、ConcurrentHashMap的put性能没有HashMap的高,get的性能与HashMap一样或者略高于,但是是线程安全的
参考:http://blog.sina.com.cn/s/blog_56fd58ab0100qel3.html
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)
(watch = new StopWatch()).start();
for (int i = 0; i < loop; i++) {
j = (int) (Math.random() * loop);
map.put(j, new Integer(-j));
}
watch.stop();
out.printf("d", watch.getTime());
//3.测试随机索引删除(Random remove)
(watch = new StopWatch()).start();
for (int i = 0; i < loop; i++) {
j = (int) (Math.random() * loop);
map.remove(j);
}
watch.stop();
out.printf("d", watch.getTime());
//4.测试随机取数性能(Random get)
(watch = new StopWatch()).start();
for (int i = 0; i < loop; i++) {
j = (int) (Math.random() * loop);
map.get(j);
}
watch.stop();
out.printf("d", watch.getTime());
//6.测试迭代性能(Iterator)
(watch = new StopWatch()).start();
Iterator<Object> iter = map.values().iterator();
while (iter.hasNext()) {
iter.next();
}
watch.stop();
out.printf("d", watch.getTime());
//7.测试迭代性能(Iterator)
(watch = new StopWatch()).start();
// Iterator<Object> iter = list.iterator();
for (Object obj : map.values()) {
}
watch.stop();
out.printf("d", watch.getTime());
}
}
结果:
-----------------------Test Map Performance: loop=100000------------------------
put random-put remove get
iterator for
HashMap 47 94 78
47 0 0
Hashtable 141 78 78 62
0 16
LinkedHashMap 125 78 94 47
0 0
IdentityHashMap 172 265 47 63 15
16
TreeMap 94 125 187 109
0 0
WeakHashMap 156 250 47 31
0 16
ConcurrentHashMap 94 140 79 46 0
16
结论:
1、HashMap的性能比较高,但不是线程安全的
2、ConcurrentHashMap的put性能没有HashMap的高,get的性能与HashMap一样或者略高于,但是是线程安全的
参考:http://blog.sina.com.cn/s/blog_56fd58ab0100qel3.html
相关文章推荐
- Java高级个人笔记(判断List、Map、Set是否为空及效率比较)
- java hasmap对象的深复制实现:字节码复制和对象序列化成字符串复制比较。
- java中两个对象间的属性值复制,比较,转为map方法实现
- Java中Comparable和Comparator实现对象比较
- Java中迭代列表中数据时几种循环写法的效率比较
- Python 的 map、列表推导、循环效率比较
- JAVA-JSP内置对象之request获得封装所有参数值的Map
- java中将list、map对象写入文件
- java 几个Map的性能比较
- Java:Map与HashMap,Hashtable,HashSet比较
- java对象与map对象相互转换
- 问个比较菜的问题,在JAVA中new完对象后有没有办法消除对象,节省手机内存。
- 互斥对象锁和临界区锁性能比较(临界区效率更高,互斥可用于多进程的多线程中)
- JAVA [ 类对象比较 ]
- Java为什么两个值相等的对象==比较返回false而两个值相等的不同类型的的基本数据类型返回true
- java操作Excel效率之poi与fastexcel比较
- xml,json,map,java对象互相转换解析
- Java中如何遍历Map对象的4种方法
- java map按照value值来比较大小并且返回最终结果
- Java里多个Map的性能比较(TreeMap、HashMap、ConcurrentSkipListMap)