1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.collections4;
18
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.Set;
23
24 import org.apache.commons.collections4.map.Flat3Map;
25
26 /**
27 * {@code MapPerformanceTest} is designed to perform basic Map performance tests.
28 */
29 public class MapPerformance {
30
31 private static final class DummyMap<K, V> implements Map<K, V> {
32 @Override
33 public void clear() {
34 }
35 @Override
36 public boolean containsKey(final Object key) {
37 return false;
38 }
39 @Override
40 public boolean containsValue(final Object value) {
41 return false;
42 }
43 @Override
44 public Set<Map.Entry<K, V>> entrySet() {
45 return null;
46 }
47 @Override
48 public V get(final Object key) {
49 return null;
50 }
51 @Override
52 public boolean isEmpty() {
53 return false;
54 }
55 @Override
56 public Set<K> keySet() {
57 return null;
58 }
59 @Override
60 public V put(final K key, final V value) {
61 return null;
62 }
63 @Override
64 public void putAll(final Map<? extends K, ? extends V> t) {
65 }
66 @Override
67 public V remove(final Object key) {
68 return null;
69 }
70 @Override
71 public int size() {
72 return 0;
73 }
74 @Override
75 public Collection<V> values() {
76 return null;
77 }
78 }
79
80 /** The total number of runs for each test */
81 private static final int RUNS = 20000000;
82
83 /**
84 * Main method
85 */
86 public static void main(final String[] args) {
87 testAll();
88 }
89
90 private static void test(final Map<String, String> map, final String name) {
91 long startMillis = 0;
92 long endMillis = 0;
93 // int total = 0;
94 startMillis = System.currentTimeMillis();
95 for (int i = RUNS; i > 0; i--) {
96 // if (map.get("Alpha") != null) total++;
97 // if (map.get("Beta") != null) total++;
98 // if (map.get("Gamma") != null) total++;
99 map.put("Alpha", "A");
100 map.put("Beta", "B");
101 map.put("Beta", "C");
102 map.put("Gamma", "D");
103 // map.remove("Gamma");
104 // map.remove("Beta");
105 // map.remove("Alpha");
106 map.put("Delta", "E");
107 map.clear();
108 }
109 endMillis = System.currentTimeMillis();
110 System.out.println(name + (endMillis - startMillis));
111 }
112
113 private static void testAll() {
114 final Map<String, String> dummyMap = new DummyMap<>();
115 final Map<String, String> hashMap = new HashMap<>();
116 // hashMap.put("Alpha", "A");
117 // hashMap.put("Beta", "B");
118 // hashMap.put("Gamma", "C");
119 // hashMap.put("Delta", "D");
120 final Map<String, String> flatMap = new Flat3Map<>(hashMap);
121 System.out.println(flatMap);
122 // Map<String, String> unmodHashMap = Collections.unmodifiableMap(new HashMap<String, String>(hashMap));
123 // Map fastHashMap = new FastHashMap(hashMap);
124 // Map<String, String> treeMap = new TreeMap<String, String>(hashMap);
125 // Map linkedMap = new LinkedHashMap(hashMap);
126 // Map syncMap = Collections.unmodifiableMap(new HashMap(hashMap));
127 // Map bucketMap = new StaticBucketMap();
128 // bucketMap.putAll(hashMap);
129 // Map doubleMap = new DoubleOrderedMap(hashMap);
130
131 // dummy is required as the VM seems to hotspot the first call to the
132 // test method with the given type
133 test(dummyMap, " Dummy ");
134 test(dummyMap, " Dummy ");
135 test(dummyMap, " Dummy ");
136 test(flatMap, " Flat3 ");
137 test(hashMap, " HashMap ");
138
139 test(flatMap, " Flat3 ");
140 test(flatMap, " Flat3 ");
141 test(flatMap, " Flat3 ");
142
143 test(hashMap, " HashMap ");
144 test(hashMap, " HashMap ");
145 test(hashMap, " HashMap ");
146
147 // test(treeMap, " TreeMap ");
148 // test(treeMap, " TreeMap ");
149 // test(treeMap, " TreeMap ");
150
151 // test(unmodHashMap, "Unmod(HashMap) ");
152 // test(unmodHashMap, "Unmod(HashMap) ");
153 // test(unmodHashMap, "Unmod(HashMap) ");
154 //
155 // test(syncMap, " Sync(HashMap) ");
156 // test(syncMap, " Sync(HashMap) ");
157 // test(syncMap, " Sync(HashMap) ");
158 //
159 // test(fastHashMap, " FastHashMap ");
160 // test(fastHashMap, " FastHashMap ");
161 // test(fastHashMap, " FastHashMap ");
162 //
163 // test(seqMap, " SeqHashMap ");
164 // test(seqMap, " SeqHashMap ");
165 // test(seqMap, " SeqHashMap ");
166 //
167 // test(linkedMap, " LinkedHashMap ");
168 // test(linkedMap, " LinkedHashMap ");
169 // test(linkedMap, " LinkedHashMap ");
170 //
171 // test(bucketMap, " BucketMap ");
172 // test(bucketMap, " BucketMap ");
173 // test(bucketMap, " BucketMap ");
174 //
175 // test(doubleMap, " DoubleMap ");
176 // test(doubleMap, " DoubleMap ");
177 // test(doubleMap, " DoubleMap ");
178 }
179
180 }
181