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)  
        (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
           Hashtable       141        78        78        62               16
       LinkedHashMap       125        78        94        47                0
     IdentityHashMap       172       265        47        63        15        16
             TreeMap        94       125       187       109                0
         WeakHashMap       156       250        47        31               16
   ConcurrentHashMap        94       140        79        46               16

结论:

   1、HashMap的性能比较高,但不是线程安全的

   2、ConcurrentHashMap的put性能没有HashMap的高,get的性能与HashMap一样或者略高于,但是是线程安全的

 

参考:http://blog.sina.com.cn/s/blog_56fd58ab0100qel3.html

原创粉丝点击