1 16 package org.apache.commons.collections; 17 18 import java.util.Collection ; 19 import java.util.Collections ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 import java.util.Set ; 23 import java.util.TreeMap ; 24 25 import org.apache.commons.collections.map.Flat3Map; 26 27 32 public class MapPerformance { 33 34 35 private static final int RUNS = 20000000; 36 37 40 public static void main(String [] args) { 41 testAll(); 42 } 43 44 private static void testAll() { 45 Map dummyMap = new DummyMap(); 46 Map hashMap = new HashMap (); 47 Map flatMap = new Flat3Map(hashMap); 52 System.out.println(flatMap); 53 Map unmodHashMap = Collections.unmodifiableMap(new HashMap (hashMap)); 54 Map fastHashMap = new FastHashMap(hashMap); 55 Map treeMap = new TreeMap (hashMap); 56 Map seqMap = new SequencedHashMap(hashMap); 57 63 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 83 } 111 112 private static void test(Map map, String name) { 113 long start = 0, end = 0; 114 int total = 0; 115 start = System.currentTimeMillis(); 116 for (int i = RUNS; i > 0; i--) { 117 map.put("Alpha", "A"); 121 map.put("Beta", "B"); 122 map.put("Beta", "C"); 123 map.put("Gamma", "D"); 124 map.put("Delta", "E"); 128 map.clear(); 129 } 130 end = System.currentTimeMillis(); 131 System.out.println(name + (end - start)); 132 } 133 134 136 private static class DummyMap implements Map { 137 public void clear() { 138 } 139 public boolean containsKey(Object key) { 140 return false; 141 } 142 public boolean containsValue(Object value) { 143 return false; 144 } 145 public Set entrySet() { 146 return null; 147 } 148 public Object get(Object key) { 149 return null; 150 } 151 public boolean isEmpty() { 152 return false; 153 } 154 public Set keySet() { 155 return null; 156 } 157 public Object put(Object key, Object value) { 158 return null; 159 } 160 public void putAll(Map t) { 161 } 162 public Object remove(Object key) { 163 return null; 164 } 165 public int size() { 166 return 0; 167 } 168 public Collection values() { 169 return null; 170 } 171 } 172 173 } 174 175 | Popular Tags |