HashMap vs FastHashMap

2010/03/24 14:17
阅读数 1.5K

今日在无意之中发现了FastHashMap这个东西,是apache的commons项目的工具类。

是不是真的如名字一样Faster than HashMap呢,,我们做个实验

 

Test Code:

 

 import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.commons.collections.FastHashMap;
 
 /**
  *

 

打印结果是

HashMap put 1000000 Object using1.453s
FastHashMap put 1000000 Object using2.094s
HashMap get 1000000 Object using0.094s
FastHashMap get 1000000 Object using0.062s
哗,果然是快了哦,,快0.03秒,呵呵

我把1000000 改成50000结果就是互有领先。

 

 

HashMapTester.java


  *
  *


  * View Source
  *


  *
  * @author $Author$
  * @version $Reversion$ $Date$
  */
 public class HashMapTester {
 
     /**
      *
      */
     public HashMapTester() {
         super();
     }
    
     public static void main(String[] args){
         int N = 1000000;
         long start = System.currentTimeMillis();
         Map hm = new HashMap(N);
         for(int i = 0; i < N ; i++){
             hm.put(new Long(i),"HashMap " + i);
         }
         long end = System.currentTimeMillis() - start;
         System.out.println("HashMap put " + N + " Object using" + (end/1000.0) + "s");
         start = System.currentTimeMillis();
         FastHashMap fhm = new FastHashMap(N);
         //fhm.setFast(false);
         for(int i = 0; i < N ; i++){
             fhm.put(new Long(i),"FastHashMap " + i);
         }
         end = System.currentTimeMillis() - start;
         System.out.println("FastHashMap put " + N + " Object using" + (end/1000.0) + "s");
        
        
         start = System.currentTimeMillis();
         for(int i = 0; i < N ; i++){
             hm.get(new Long(i));
         }
         end = System.currentTimeMillis() - start;
         System.out.println("HashMap get " + N + " Object using" + (end/1000.0) + "s");
         fhm.setFast(true);
         start = System.currentTimeMillis();
         for(int i = 0; i < N ; i++){
             fhm.get(new Long(i));
         }
         end = System.currentTimeMillis() - start;
         System.out.println("FastHashMap get " + N + " Object using" + (end/1000.0) + "s");
        
     }
 
 }

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部