Skip to content

Commit

Permalink
Issue #3681
Browse files Browse the repository at this point in the history
More benchmark variations

Signed-off-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
gregw committed May 22, 2019
1 parent 2d358cd commit ab86703
Showing 1 changed file with 62 additions and 13 deletions.
Expand Up @@ -20,11 +20,14 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
Expand Down Expand Up @@ -64,6 +67,7 @@ public class ListVsMapBenchmark
static final String base = "This-is-the-base-of-All-key-names-and-is-long".substring(0,length);
static final String miss = "X-" + base;
static final List<String> trials = new ArrayList<>();
static final Random random = new Random();

@Setup(Level.Trial)
public void setup()
Expand All @@ -78,9 +82,8 @@ public void setup()
default : throw new IllegalStateException();
}

int hit = size / 2;
for (int h = hits; h-->0;)
trials.add(base + "-" + ((hit++) % size));
trials.add(base + "-" + (h % size));

for (int m = misses; m-->0; )
trials.add(miss);
Expand All @@ -92,9 +95,9 @@ public void setup()
static class Pair
{
final String key;
final long value;
final String value;

public Pair(String key, long value)
public Pair(String key, String value)
{
this.key = key;
this.value = value;
Expand All @@ -117,7 +120,7 @@ private void fill(Fill fill)
for (int i=0; i<size; i++)
{
String key = base + "-" + i;
Pair pair = new Pair(key, key.hashCode());
Pair pair = new Pair(key, Long.toString(random.nextLong(),8));
fill.put(pair);
}
}
Expand All @@ -141,13 +144,13 @@ private long test(Lookup lookup)
Pair p = i.next();
String k = p.key;
if (one != null && one.equals(k))
result ^= p.value;
result ^= p.value.hashCode();
else if (two != null && one.equals(k))
result ^= p.value;
result ^= p.value.hashCode();
else if (three != null && one.equals(k))
result ^= p.value;
result ^= p.value.hashCode();
else if (four != null && one.equals(k))
result ^= p.value;
result ^= p.value.hashCode();
}
}
}
Expand All @@ -157,7 +160,7 @@ else if (four != null && one.equals(k))
{
Pair p = lookup.get(t);
if (p != null)
result ^= p.value;
result ^= p.value.hashCode();
}
}

Expand Down Expand Up @@ -189,7 +192,7 @@ public Iterator<Pair> iterate()

@Benchmark
@BenchmarkMode({Mode.Throughput})
public long testList() throws Exception
public long testArrayList() throws Exception
{
List<Pair> list = new ArrayList<>(size);
fill(list::add);
Expand All @@ -198,12 +201,22 @@ public long testList() throws Exception

@Benchmark
@BenchmarkMode({Mode.Throughput})
public long testSortedMap() throws Exception
public long testLinkedList() throws Exception
{
List<Pair> list = new LinkedList<>();
fill(list::add);
return listLookup(list);
}

@Benchmark
@BenchmarkMode({Mode.Throughput})
public long testLinkedHashMap() throws Exception
{
// This loses the true ordering of fields
Map<String,List<Pair>> map = new LinkedHashMap<>(size);
fill(p->
{
List<Pair> list = new ArrayList<>();
List<Pair> list = new LinkedList<>();
list.add(p);
map.put(p.key.toLowerCase(),list);
});
Expand Down Expand Up @@ -245,6 +258,42 @@ public Pair next()
});
}


@Benchmark
@BenchmarkMode({Mode.Throughput})
public long testHashMapAndLinkedList() throws Exception
{
// This keeps the true ordering of fields
Map<String,List<Pair>> map = new HashMap<>(size);
List<Pair> order = new LinkedList<>();

fill(p->
{
List<Pair> list = new LinkedList<>();
list.add(p);
map.put(p.key.toLowerCase(),list);
order.add(p);
});
return test(new Lookup()
{
@Override
public Pair get(String k)
{
List<Pair> list = map.get(k.toLowerCase());
if (list == null || list.isEmpty())
return null;
return list.get(0);
}

@Override
public Iterator<Pair> iterate()
{
return order.iterator();
}
});
}


public static void main(String[] args) throws RunnerException
{
Options opt = new OptionsBuilder()
Expand Down

0 comments on commit ab86703

Please sign in to comment.