- import static java.lang.System.out;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.Hashtable;
- import java.util.IdentityHashMap;
- import java.util.Iterator;
- import java.util.LinkedHashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import java.util.Stack;
- import java.util.TreeMap;
- import java.util.Vector;
- import java.util.WeakHashMap;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.concurrent.CopyOnWriteArrayList;
- import org.apache.commons.collections.FastArrayList;
- import org.apache.commons.collections.list.TreeList;
- import org.apache.commons.lang.StringUtils;
- import org.apache.commons.lang.time.StopWatch;
- @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一样或者略高于,但是是线程安全的