001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections;
017:
018: import java.util.Collection;
019: import java.util.Collections;
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.Set;
023: import java.util.TreeMap;
024:
025: import org.apache.commons.collections.map.Flat3Map;
026:
027: /**
028: * <code>TestMapPerformance</code> is designed to perform basic Map performance tests.
029: *
030: * @author Stephen Colebourne
031: */
032: public class MapPerformance {
033:
034: /** The total number of runs for each test */
035: private static final int RUNS = 20000000;
036:
037: /**
038: * Main method
039: */
040: public static void main(String[] args) {
041: testAll();
042: }
043:
044: private static void testAll() {
045: Map dummyMap = new DummyMap();
046: Map hashMap = new HashMap();
047: // hashMap.put("Alpha", "A");
048: // hashMap.put("Beta", "B");
049: // hashMap.put("Gamma", "C");
050: // hashMap.put("Delta", "D");
051: Map flatMap = new Flat3Map(hashMap);
052: System.out.println(flatMap);
053: Map unmodHashMap = Collections.unmodifiableMap(new HashMap(
054: hashMap));
055: Map fastHashMap = new FastHashMap(hashMap);
056: Map treeMap = new TreeMap(hashMap);
057: Map seqMap = new SequencedHashMap(hashMap);
058: // Map linkedMap = new LinkedHashMap(hashMap);
059: // Map syncMap = Collections.unmodifiableMap(new HashMap(hashMap));
060: // Map bucketMap = new StaticBucketMap();
061: // bucketMap.putAll(hashMap);
062: // Map doubleMap = new DoubleOrderedMap(hashMap);
063:
064: // dummy is required as the VM seems to hotspot the first call to the
065: // test method with the given type
066: test(dummyMap, " Dummy ");
067: test(dummyMap, " Dummy ");
068: test(dummyMap, " Dummy ");
069: test(flatMap, " Flat3 ");
070: test(hashMap, " HashMap ");
071:
072: test(flatMap, " Flat3 ");
073: test(flatMap, " Flat3 ");
074: test(flatMap, " Flat3 ");
075:
076: test(hashMap, " HashMap ");
077: test(hashMap, " HashMap ");
078: test(hashMap, " HashMap ");
079:
080: // test(treeMap, " TreeMap ");
081: // test(treeMap, " TreeMap ");
082: // test(treeMap, " TreeMap ");
083:
084: // test(unmodHashMap, "Unmod(HashMap) ");
085: // test(unmodHashMap, "Unmod(HashMap) ");
086: // test(unmodHashMap, "Unmod(HashMap) ");
087: //
088: // test(syncMap, " Sync(HashMap) ");
089: // test(syncMap, " Sync(HashMap) ");
090: // test(syncMap, " Sync(HashMap) ");
091: //
092: // test(fastHashMap, " FastHashMap ");
093: // test(fastHashMap, " FastHashMap ");
094: // test(fastHashMap, " FastHashMap ");
095: //
096: // test(seqMap, " SeqHashMap ");
097: // test(seqMap, " SeqHashMap ");
098: // test(seqMap, " SeqHashMap ");
099: //
100: // test(linkedMap, " LinkedHashMap ");
101: // test(linkedMap, " LinkedHashMap ");
102: // test(linkedMap, " LinkedHashMap ");
103: //
104: // test(bucketMap, " BucketMap ");
105: // test(bucketMap, " BucketMap ");
106: // test(bucketMap, " BucketMap ");
107: //
108: // test(doubleMap, " DoubleMap ");
109: // test(doubleMap, " DoubleMap ");
110: // test(doubleMap, " DoubleMap ");
111: }
112:
113: private static void test(Map map, String name) {
114: long start = 0, end = 0;
115: int total = 0;
116: start = System.currentTimeMillis();
117: for (int i = RUNS; i > 0; i--) {
118: // if (map.get("Alpha") != null) total++;
119: // if (map.get("Beta") != null) total++;
120: // if (map.get("Gamma") != null) total++;
121: map.put("Alpha", "A");
122: map.put("Beta", "B");
123: map.put("Beta", "C");
124: map.put("Gamma", "D");
125: // map.remove("Gamma");
126: // map.remove("Beta");
127: // map.remove("Alpha");
128: map.put("Delta", "E");
129: map.clear();
130: }
131: end = System.currentTimeMillis();
132: System.out.println(name + (end - start));
133: }
134:
135: // ----------------------------------------------------------------------
136:
137: private static class DummyMap implements Map {
138: public void clear() {
139: }
140:
141: public boolean containsKey(Object key) {
142: return false;
143: }
144:
145: public boolean containsValue(Object value) {
146: return false;
147: }
148:
149: public Set entrySet() {
150: return null;
151: }
152:
153: public Object get(Object key) {
154: return null;
155: }
156:
157: public boolean isEmpty() {
158: return false;
159: }
160:
161: public Set keySet() {
162: return null;
163: }
164:
165: public Object put(Object key, Object value) {
166: return null;
167: }
168:
169: public void putAll(Map t) {
170: }
171:
172: public Object remove(Object key) {
173: return null;
174: }
175:
176: public int size() {
177: return 0;
178: }
179:
180: public Collection values() {
181: return null;
182: }
183: }
184:
185: }
|