HashMap vs FastHashMap

类别:Java 点击:0 评论:0 推荐:

Test Code:
01 import java.util.HashMap;
02 import java.util.Map;
03 
04 import org.apache.commons.collections.FastHashMap;
05 
06 /**
07  * <p>HashMapTester.java</p>
08  * 
09  * <p>
10  * <a href="HashMapTester.java.html"><i>View Source</i></a>
11  * </p>
12  * 
13  * @author $Author$
14  * @version $Reversion$ $Date$
15  */
16 public class HashMapTester {
17 
18     /**
19      * 
20      */
21     public HashMapTester() {
22         super();
23     }
24     
25     public static void main(String[] args){
26         int N = 50000;
27         long start = System.currentTimeMillis();
28         Map hm = new HashMap(N);
29         for(int i = 0; i < N ; i++){
30             hm.put(new Long(i),"HashMap " + i);
31         }
32         long end = System.currentTimeMillis() - start;
33         System.out.println("HashMap put " + N + " Object using" + (end/1000.0) + "s");
34         start = System.currentTimeMillis();
35         FastHashMap fhm = new FastHashMap(N);
36         //fhm.setFast(false);
37         for(int i = 0; i < N ; i++){
38             fhm.put(new Long(i),"FastHashMap " + i);
39         }
40         end = System.currentTimeMillis() - start;
41         System.out.println("FastHashMap put " + N + " Object using" + (end/1000.0) + "s");
42         
43         
44         start = System.currentTimeMillis();
45         for(int i = 0; i < N ; i++){
46             hm.get(new Long(i));
47         }
48         end = System.currentTimeMillis() - start;
49         System.out.println("HashMap get " + N + " Object using" + (end/1000.0) + "s");
50         fhm.setFast(true);
51         start = System.currentTimeMillis();
52         for(int i = 0; i < N ; i++){
53             fhm.get(new Long(i));
54         }
55         end = System.currentTimeMillis() - start;
56         System.out.println("FastHashMap get " + N + " Object using" + (end/1000.0) + "s");
57         
58     }
59 
60 }

Result:

HashMap put 50000 Object using1.021s FastHashMap put 50000 Object using1.221s HashMap get 50000 Object using0.561s FastHashMap get 50000 Object using0.04s

本文地址:http://com.8s8s.com/it/it15181.htm