KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > MapPerformance


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23 import java.util.TreeMap JavaDoc;
24
25 import org.apache.commons.collections.map.Flat3Map;
26
27 /**
28  * <code>TestMapPerformance</code> is designed to perform basic Map performance tests.
29  *
30  * @author Stephen Colebourne
31  */

32 public class MapPerformance {
33
34     /** The total number of runs for each test */
35     private static final int RUNS = 20000000;
36     
37     /**
38      * Main method
39      */

40     public static void main(String JavaDoc[] args) {
41         testAll();
42     }
43     
44     private static void testAll() {
45         Map JavaDoc dummyMap = new DummyMap();
46         Map JavaDoc hashMap = new HashMap JavaDoc();
47 // hashMap.put("Alpha", "A");
48
// hashMap.put("Beta", "B");
49
// hashMap.put("Gamma", "C");
50
// hashMap.put("Delta", "D");
51
Map JavaDoc flatMap = new Flat3Map(hashMap);
52         System.out.println(flatMap);
53         Map JavaDoc unmodHashMap = Collections.unmodifiableMap(new HashMap JavaDoc(hashMap));
54         Map JavaDoc fastHashMap = new FastHashMap(hashMap);
55         Map JavaDoc treeMap = new TreeMap JavaDoc(hashMap);
56         Map JavaDoc seqMap = new SequencedHashMap(hashMap);
57 // Map linkedMap = new LinkedHashMap(hashMap);
58
// Map syncMap = Collections.unmodifiableMap(new HashMap(hashMap));
59
// Map bucketMap = new StaticBucketMap();
60
// bucketMap.putAll(hashMap);
61
// Map doubleMap = new DoubleOrderedMap(hashMap);
62

63         // dummy is required as the VM seems to hotspot the first call to the
64
// test method with the given type
65
test(dummyMap, " Dummy ");
66         test(dummyMap, " Dummy ");
67         test(dummyMap, " Dummy ");
68         test(flatMap, " Flat3 ");
69         test(hashMap, " HashMap ");
70         
71         test(flatMap, " Flat3 ");
72         test(flatMap, " Flat3 ");
73         test(flatMap, " Flat3 ");
74         
75         test(hashMap, " HashMap ");
76         test(hashMap, " HashMap ");
77         test(hashMap, " HashMap ");
78         
79 // test(treeMap, " TreeMap ");
80
// test(treeMap, " TreeMap ");
81
// test(treeMap, " TreeMap ");
82

83 // test(unmodHashMap, "Unmod(HashMap) ");
84
// test(unmodHashMap, "Unmod(HashMap) ");
85
// test(unmodHashMap, "Unmod(HashMap) ");
86
//
87
// test(syncMap, " Sync(HashMap) ");
88
// test(syncMap, " Sync(HashMap) ");
89
// test(syncMap, " Sync(HashMap) ");
90
//
91
// test(fastHashMap, " FastHashMap ");
92
// test(fastHashMap, " FastHashMap ");
93
// test(fastHashMap, " FastHashMap ");
94
//
95
// test(seqMap, " SeqHashMap ");
96
// test(seqMap, " SeqHashMap ");
97
// test(seqMap, " SeqHashMap ");
98
//
99
// test(linkedMap, " LinkedHashMap ");
100
// test(linkedMap, " LinkedHashMap ");
101
// test(linkedMap, " LinkedHashMap ");
102
//
103
// test(bucketMap, " BucketMap ");
104
// test(bucketMap, " BucketMap ");
105
// test(bucketMap, " BucketMap ");
106
//
107
// test(doubleMap, " DoubleMap ");
108
// test(doubleMap, " DoubleMap ");
109
// test(doubleMap, " DoubleMap ");
110
}
111
112     private static void test(Map JavaDoc map, String JavaDoc name) {
113         long start = 0, end = 0;
114         int total = 0;
115         start = System.currentTimeMillis();
116         for (int i = RUNS; i > 0; i--) {
117 // if (map.get("Alpha") != null) total++;
118
// if (map.get("Beta") != null) total++;
119
// if (map.get("Gamma") != null) total++;
120
map.put("Alpha", "A");
121             map.put("Beta", "B");
122             map.put("Beta", "C");
123             map.put("Gamma", "D");
124 // map.remove("Gamma");
125
// map.remove("Beta");
126
// map.remove("Alpha");
127
map.put("Delta", "E");
128             map.clear();
129         }
130         end = System.currentTimeMillis();
131         System.out.println(name + (end - start));
132     }
133
134     // ----------------------------------------------------------------------
135

136     private static class DummyMap implements Map JavaDoc {
137         public void clear() {
138         }
139         public boolean containsKey(Object JavaDoc key) {
140             return false;
141         }
142         public boolean containsValue(Object JavaDoc value) {
143             return false;
144         }
145         public Set JavaDoc entrySet() {
146             return null;
147         }
148         public Object JavaDoc get(Object JavaDoc key) {
149             return null;
150         }
151         public boolean isEmpty() {
152             return false;
153         }
154         public Set JavaDoc keySet() {
155             return null;
156         }
157         public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
158             return null;
159         }
160         public void putAll(Map JavaDoc t) {
161         }
162         public Object JavaDoc remove(Object JavaDoc key) {
163             return null;
164         }
165         public int size() {
166             return 0;
167         }
168         public Collection JavaDoc values() {
169             return null;
170         }
171     }
172     
173 }
174
175
Popular Tags