@Benchmark boolean get(int reps) { // Paranoia: acting on hearsay that accessing fields might be slow // Should write a benchmark to test that! Map<Element, Element> map = mapToTest; Element[] queries = this.queries; // Allows us to use & instead of %, acting on hearsay that division // operators (/%) are disproportionately expensive; should test this too! int mask = queries.length - 1; boolean dummy = false; for (int i = 0; i < reps; i++) { dummy ^= map.get(queries[i & mask]) != null; } return dummy; }
@Benchmark int iteration(int reps) { int x = 0; for (int i = 0; i < reps; i++) { for (Element y : setToTest) { x ^= System.identityHashCode(y); } } return x; }
@Override Map<Element, Element> create(Collection<Element> keys) { Map<Element, Element> map = Maps.newHashMap(); for (Element element: keys) { map.put(element, element); } return map; }
@Override Map<Element, Element> create(Collection<Element> keys) { Map<Element, Element> map = Maps.newLinkedHashMap(); for (Element element: keys) { map.put(element, element); } return map; }
@Override Map<Element, Element> create(Collection<Element> keys) { Map<Element, Element> map = Maps.newTreeMap(); for (Element element: keys) { map.put(element, element); } return map; }
@Override Map<Element, Element> create(Collection<Element> keys) { Map<Element, Element> map = new ConcurrentSkipListMap<Element, Element>(); for (Element element: keys) { map.put(element, element); } return map; }
@Override Map<Element, Element> create(Collection<Element> keys) { Map<Element, Element> map = new ConcurrentHashMap<Element, Element>(keys.size(), 0.75f, 1); for (Element element: keys) { map.put(element, element); } return map; }
@Override Map<Element, Element> create(Collection<Element> keys) { Map<Element, Element> map = new ConcurrentHashMap<Element, Element>(keys.size(), 0.75f, 16); for (Element element: keys) { map.put(element, element); } return map; }
@Override Map<Element, Element> create(Collection<Element> keys) { Map<Element, Element> map = new MapMaker() .concurrencyLevel(1) .makeMap(); for (Element element: keys) { map.put(element, element); } return map; }
@Override Map<Element, Element> create(Collection<Element> keys) { Map<Element, Element> map = new MapMaker() .concurrencyLevel(16) .makeMap(); for (Element element: keys) { map.put(element, element); } return map; }
@Override Map<Element, Element> create(Collection<Element> keys) { ImmutableMap.Builder<Element, Element> builder = ImmutableMap.builder(); for (Element element : keys) { builder.put(element, element); } return builder.build(); }
@Override Map<Element, Element> create(Collection<Element> keys) { ImmutableSortedMap.Builder<Element, Element> builder = ImmutableSortedMap.naturalOrder(); for (Element element : keys) { builder.put(element, element); } return builder.build(); }
@BeforeExperiment void setUp() { CollectionBenchmarkSampleData sampleData = new CollectionBenchmarkSampleData( isUserTypeFast, random, hitRate, size); if (sortedData) { List<Element> valueList = newArrayList(sampleData.getValuesInSet()); Collections.sort(valueList); values = valueList; } else { values = sampleData.getValuesInSet(); } this.mapToTest = impl.create(values); this.queries = sampleData.getQueries(); }
@Benchmark boolean iterateWithEntrySet(int reps) { Map<Element, Element> map = mapToTest; boolean dummy = false; for (int i = 0; i < reps; i++) { for (Map.Entry<Element, Element> entry : map.entrySet()) { dummy ^= entry.getKey() != entry.getValue(); } } return dummy; }
@Benchmark boolean iterateWithKeySetAndGet(int reps) { Map<Element, Element> map = mapToTest; boolean dummy = false; for (int i = 0; i < reps; i++) { for (Element key : map.keySet()) { Element value = map.get(key); dummy ^= key != value; } } return dummy; }
@Benchmark boolean iterateValuesAndGet(int reps) { Map<Element, Element> map = mapToTest; boolean dummy = false; for (int i = 0; i < reps; i++) { for (Element key : map.values()) { // This normally wouldn't make sense, but because our keys are our values it kind of does Element value = map.get(key); dummy ^= key != value; } } return dummy; }
@BeforeExperiment void setUp() { CollectionBenchmarkSampleData sampleData = new CollectionBenchmarkSampleData( isUserTypeFast, random, hitRate, size); this.setToTest = (Set<Element>)impl.create(sampleData.getValuesInSet()); this.queries = sampleData.getQueries(); }
@Benchmark boolean contains(int reps) { // Paranoia: acting on hearsay that accessing fields might be slow // Should write a benchmark to test that! Set<Element> set = setToTest; Element[] queries = this.queries; int mask = queries.length - 1; boolean dummy = false; for (int i = 0; i < reps; i++) { dummy ^= set.contains(queries[i & mask]); } return dummy; }