Skip to content

Instantly share code, notes, and snippets.

@benjaminmullard
Created May 19, 2015 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjaminmullard/6de5415a1b58f0940a65 to your computer and use it in GitHub Desktop.
Save benjaminmullard/6de5415a1b58f0940a65 to your computer and use it in GitHub Desktop.
Test performance of Map implementations
package uk.co.validus.billingserver.util;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.collections4.map.LinkedMap;
public class MapPerformanceTest
{
public static final int MAP_SIZE = 1000000;
public static void main(String[] args)
{
testPerformance("Collections.synchronizedMap backed by LinkedHashMap", Collections.synchronizedMap(new LinkedHashMap<Integer, String>()));
testPerformance("Collections.synchronizedMap backed by LinkedMap", Collections.synchronizedMap(new LinkedMap<Integer, String>()));
testPerformance("ConcurrentHashMap", new ConcurrentHashMap<Integer, String>());
testPerformance("TreeMap", new TreeMap<Integer, String>());
}
public static void testPerformance(String testName, Map<Integer, String> testMap)
{
System.out.println("Starting test for " + testName + " performance");
final long start = System.currentTimeMillis();
for (int i = 0; i < MAP_SIZE; i++)
{
testMap.put(i, "Test item " + i);
}
System.out.println("\tTime to populate map: " + (System.currentTimeMillis() - start) + "ms");
testMap.get(Double.valueOf(Math.floor(MAP_SIZE / 2)).intValue());
System.out.println("\tTime to find midpoint value: " + (System.currentTimeMillis() - start) + "ms");
final Iterator<Entry<Integer, String>> iter = testMap.entrySet().iterator();
while (iter.hasNext())
{
iter.next();
}
System.out.println("\tTime to iterate map: " + (System.currentTimeMillis() - start) + "ms");
System.out.println("\tTime to complete test: " + (System.currentTimeMillis() - start) + "ms");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment