Created
July 26, 2012 03:24
-
-
Save esamson/3180055 to your computer and use it in GitHub Desktop.
Microbenchmark of `put()` and `get()` operations of Java Map implementations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.math.*; | |
public class MapVsMap { | |
static final int MAP_SIZE = 100000; | |
static final int STR_LEN = 10; | |
static final int ROUNDS = 10; | |
static final char[] chars = { | |
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', | |
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', | |
'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', | |
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', | |
'5', '6', '7', '8', '9', '0' | |
}; | |
static final Random random = new Random(); | |
static String randomString(int length) { | |
StringBuilder s = new StringBuilder(length); | |
for (int i = 0; i < length; i++) { | |
s.append(chars[random.nextInt(chars.length)]); | |
} | |
return s.toString(); | |
} | |
public static void main(String[] args) { | |
LinkedList<String> strings = new LinkedList<String>(); | |
for (int i = 0; i < MAP_SIZE; i++) { | |
strings.add(randomString(STR_LEN)); | |
} | |
long start = 0; | |
long end = 0; | |
long elapsed = 0; | |
long[] hput = new long[ROUNDS]; | |
long[] hget = new long[ROUNDS]; | |
long[] lput = new long[ROUNDS]; | |
long[] lget = new long[ROUNDS]; | |
long[] tput = new long[ROUNDS]; | |
long[] tget = new long[ROUNDS]; | |
for (int round = 0; round <= ROUNDS; round++) { | |
System.out.println("\n\n=== ROUND " + round + " ==="); | |
HashMap<String, String> hm = new HashMap<String, String>(); | |
start = System.nanoTime(); | |
for (String string : strings) { | |
hm.put(string, string); | |
} | |
end = System.nanoTime(); | |
elapsed = end - start; | |
System.out.println("HashMap put: " + elapsed); | |
if (round > 0) { | |
hput[round - 1] = elapsed; | |
} | |
start = System.nanoTime(); | |
for (String string : strings) { | |
hm.get(string); | |
} | |
end = System.nanoTime(); | |
elapsed = end - start; | |
System.out.println("HashMap get: " + elapsed); | |
if (round > 0) { | |
hget[round - 1] = elapsed; | |
} | |
LinkedHashMap<String, String> lhm = new LinkedHashMap<String, String>(); | |
start = System.nanoTime(); | |
for (String string : strings) { | |
lhm.put(string, string); | |
} | |
end = System.nanoTime(); | |
elapsed = end - start; | |
System.out.println("LinkedHashMap put: " + elapsed); | |
if (round > 0) { | |
lput[round - 1] = elapsed; | |
} | |
start = System.nanoTime(); | |
for (String string : strings) { | |
lhm.get(string); | |
} | |
end = System.nanoTime(); | |
elapsed = end - start; | |
System.out.println("LinkedHashMap get: " + elapsed); | |
if (round > 0) { | |
lget[round - 1] = elapsed; | |
} | |
TreeMap<String, String> tm = new TreeMap<String, String>(); | |
start = System.nanoTime(); | |
for (String string : strings) { | |
tm.put(string, string); | |
} | |
end = System.nanoTime(); | |
elapsed = end - start; | |
System.out.println("TreeMap put: " + elapsed); | |
if (round > 0) { | |
tput[round - 1] = elapsed; | |
} | |
start = System.nanoTime(); | |
for (String string : strings) { | |
tm.get(string); | |
} | |
end = System.nanoTime(); | |
elapsed = end - start; | |
System.out.println("TreeMap get: " + elapsed); | |
if (round > 0) { | |
tget[round - 1] = elapsed; | |
} | |
} | |
Arrays.sort(hput); | |
Arrays.sort(hget); | |
Arrays.sort(lput); | |
Arrays.sort(lget); | |
Arrays.sort(tput); | |
Arrays.sort(tget); | |
System.out.println("\n\n=== RESULTS (PUT) ==="); | |
System.out.format("HashMap PUT (%s rounds):\t min: %10s; max: %10s; ave: %10s%n", | |
hput.length, hput[0], hput[hput.length - 1], ave(hput)); | |
System.out.format("LinkedHashMap PUT (%s rounds):\t min: %10s; max: %10s; ave: %10s%n", | |
lput.length, lput[0], lput[lput.length - 1], ave(lput)); | |
System.out.format("TreeMap PUT (%s rounds):\t min: %10s; max: %10s; ave: %10s%n", | |
tput.length, tput[0], tput[tput.length - 1], ave(tput)); | |
System.out.println("\n\n=== RESULTS (GET) ==="); | |
System.out.format("HashMap GET (%s rounds):\t min: %10s; max: %10s; ave: %10s%n", | |
hget.length, hget[0], hget[hget.length - 1], ave(hget)); | |
System.out.format("LinkedHashMap GET (%s rounds):\t min: %10s; max: %10s; ave: %10s%n", | |
lget.length, lget[0], lget[lget.length - 1], ave(lget)); | |
System.out.format("TreeMap GET (%s rounds):\t min: %10s; max: %10s; ave: %10s%n", | |
tget.length, tget[0], tget[tget.length - 1], ave(tget)); | |
} | |
static String ave(long[] times) { | |
BigDecimal total = BigDecimal.ZERO; | |
for (long time : times) { | |
total = total.add(new BigDecimal(time)); | |
} | |
return total.divide(new BigDecimal(times.length), MathContext.DECIMAL32).toPlainString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment