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</code> is designed to perform basic Map performance tests. 28 * 29 */ 30 public class MapPerformance { 31 32 /** The total number of runs for each test */ 33 private static final int RUNS = 20000000; 34 35 /** 36 * Main method 37 */ 38 public static void main(final String[] args) { 39 testAll(); 40 } 41 42 private static void testAll() { 43 final Map<String, String> dummyMap = new DummyMap<>(); 44 final Map<String, String> hashMap = new HashMap<>(); 45 // hashMap.put("Alpha", "A"); 46 // hashMap.put("Beta", "B"); 47 // hashMap.put("Gamma", "C"); 48 // hashMap.put("Delta", "D"); 49 final Map<String, String> flatMap = new Flat3Map<>(hashMap); 50 System.out.println(flatMap); 51 // Map<String, String> unmodHashMap = Collections.unmodifiableMap(new HashMap<String, String>(hashMap)); 52 // Map fastHashMap = new FastHashMap(hashMap); 53 // Map<String, String> treeMap = new TreeMap<String, String>(hashMap); 54 // Map linkedMap = new LinkedHashMap(hashMap); 55 // Map syncMap = Collections.unmodifiableMap(new HashMap(hashMap)); 56 // Map bucketMap = new StaticBucketMap(); 57 // bucketMap.putAll(hashMap); 58 // Map doubleMap = new DoubleOrderedMap(hashMap); 59 60 // dummy is required as the VM seems to hotspot the first call to the 61 // test method with the given type 62 test(dummyMap, " Dummy "); 63 test(dummyMap, " Dummy "); 64 test(dummyMap, " Dummy "); 65 test(flatMap, " Flat3 "); 66 test(hashMap, " HashMap "); 67 68 test(flatMap, " Flat3 "); 69 test(flatMap, " Flat3 "); 70 test(flatMap, " Flat3 "); 71 72 test(hashMap, " HashMap "); 73 test(hashMap, " HashMap "); 74 test(hashMap, " HashMap "); 75 76 // test(treeMap, " TreeMap "); 77 // test(treeMap, " TreeMap "); 78 // test(treeMap, " TreeMap "); 79 80 // test(unmodHashMap, "Unmod(HashMap) "); 81 // test(unmodHashMap, "Unmod(HashMap) "); 82 // test(unmodHashMap, "Unmod(HashMap) "); 83 // 84 // test(syncMap, " Sync(HashMap) "); 85 // test(syncMap, " Sync(HashMap) "); 86 // test(syncMap, " Sync(HashMap) "); 87 // 88 // test(fastHashMap, " FastHashMap "); 89 // test(fastHashMap, " FastHashMap "); 90 // test(fastHashMap, " FastHashMap "); 91 // 92 // test(seqMap, " SeqHashMap "); 93 // test(seqMap, " SeqHashMap "); 94 // test(seqMap, " SeqHashMap "); 95 // 96 // test(linkedMap, " LinkedHashMap "); 97 // test(linkedMap, " LinkedHashMap "); 98 // test(linkedMap, " LinkedHashMap "); 99 // 100 // test(bucketMap, " BucketMap "); 101 // test(bucketMap, " BucketMap "); 102 // test(bucketMap, " BucketMap "); 103 // 104 // test(doubleMap, " DoubleMap "); 105 // test(doubleMap, " DoubleMap "); 106 // test(doubleMap, " DoubleMap "); 107 } 108 109 private static void test(final Map<String, String> map, final String name) { 110 long start = 0, end = 0; 111 // int total = 0; 112 start = System.currentTimeMillis(); 113 for (int i = RUNS; i > 0; i--) { 114 // if (map.get("Alpha") != null) total++; 115 // if (map.get("Beta") != null) total++; 116 // if (map.get("Gamma") != null) total++; 117 map.put("Alpha", "A"); 118 map.put("Beta", "B"); 119 map.put("Beta", "C"); 120 map.put("Gamma", "D"); 121 // map.remove("Gamma"); 122 // map.remove("Beta"); 123 // map.remove("Alpha"); 124 map.put("Delta", "E"); 125 map.clear(); 126 } 127 end = System.currentTimeMillis(); 128 System.out.println(name + (end - start)); 129 } 130 131 // ---------------------------------------------------------------------- 132 133 private static class DummyMap<K, V> implements Map<K, V> { 134 @Override 135 public void clear() { 136 } 137 @Override 138 public boolean containsKey(final Object key) { 139 return false; 140 } 141 @Override 142 public boolean containsValue(final Object value) { 143 return false; 144 } 145 @Override 146 public Set<Map.Entry<K, V>> entrySet() { 147 return null; 148 } 149 @Override 150 public V get(final Object key) { 151 return null; 152 } 153 @Override 154 public boolean isEmpty() { 155 return false; 156 } 157 @Override 158 public Set<K> keySet() { 159 return null; 160 } 161 @Override 162 public V put(final K key, final V value) { 163 return null; 164 } 165 @Override 166 public void putAll(final Map<? extends K, ? extends V> t) { 167 } 168 @Override 169 public V remove(final Object key) { 170 return null; 171 } 172 @Override 173 public int size() { 174 return 0; 175 } 176 @Override 177 public Collection<V> values() { 178 return null; 179 } 180 } 181 182 } 183