Skip to content

Instantly share code, notes, and snippets.

Created March 4, 2020 06:22
Show Gist options
  • Save dimo414/e692e26e276bd9cf1749bd5d7fc800d8 to your computer and use it in GitHub Desktop.
Save dimo414/e692e26e276bd9cf1749bd5d7fc800d8 to your computer and use it in GitHub Desktop.
Benchmark of map implementation access speeds
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
* Tests the speed of accessing several different map implementations looking for the same strings.
public class MapSpeedTest {
private static long seed = System.nanoTime();
private static int loop = 100_000_000;
public static void timeAccess(Map<String,String> map) {
Random rnd = new Random(seed);
int foundCount = 0;
long start = System.nanoTime();
for(int i = 0; i < loop; i++) {
String s = map.get(;
if(s != null)
long stop = System.nanoTime() - start;
System.out.println("Found "+foundCount+" strings out of "+loop+" attempts - "+String.format("%.2f",100.0*foundCount/loop)+" success rate.");
System.out.println(map.getClass().getSimpleName()+" took "+String.format("%.4f", stop/1_000_000_000.0)+" seconds.");
public static HashMap<String,String> buildMap() {
Random rnd = new Random();
HashMap<String,String> map = new HashMap<>();
for(int i = 0; i < loop*.01; i++) {
String str =;
map.put(str, str);
return map;
public static void main(String[] args) {
// Construction / insertion time isn't relevant for this test
Map<String,String> hash = buildMap();
Map<String,String> conc = new ConcurrentHashMap<>(hash);
Map<String,String> immut = ImmutableMap.copyOf(hash);
// Shuffle the maps for good measure, doesn't seem to impact the test
List<Map<String,String>> ls = Lists.newArrayList(hash, conc, immut);
for(Map<String,String> map : ls) {
public static class RndString {
// You can play the with values here to impact the success rate
// smaller words and smaller namespaces increase the likelyhood
// a string will be in the map; conversely larger values
// mean the map will be more sparse, more often returning null.
private static final int minLen = 4;
private static final int maxLen = 5;
private static final char minChar = 'A';
private static final char maxChar = minChar+26;
private static final char[] strBuff = new char[maxLen];
public static String build(Random rnd) {
// 5 char minimum ensures reasonable likelihood of unique string
int strLen = rnd.nextInt(maxLen-minLen+1)+minLen;
for(int i = 0; i < strLen; i++) {
strBuff[i] = (char)(minChar + rnd.nextInt((int)(maxChar-minChar)));
return new String(strBuff,0,strLen);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment