Skip to content

Instantly share code, notes, and snippets.

@mhshams
Created August 2, 2013 01:44
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 mhshams/6136916 to your computer and use it in GitHub Desktop.
Save mhshams/6136916 to your computer and use it in GitHub Desktop.
final version
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author me
*/
public class StringKeyTest {
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
private static final StringGenerator GENERATOR = () -> new BigInteger(130, SECURE_RANDOM).toString(32);
public static void main(String[] args) {
List<Key> keys = new ArrayList<>();
Map<String, String> withString = new HashMap<>();
Map<Key, String> withObject = new HashMap<>();
// number of keys in the map
int n = 1000000;
// initialize the maps with auto generated random keys.
initialize(withString, withObject, keys, n);
// search by String concatenation
Finder stringFinder = key-> withString.get(key.a + "#" + key.b + "#" + key.c + "#" + key.d);
// search by StringBuilder
Finder builderFinder = key-> withString.get(new StringBuilder(key.a).append("#").append(key.b).append("#").append(key.c).append("#").append(key.d).toString());
// search by custom key object
Finder objectFinder = key -> withObject.get(new Key(key.a, key.b, key.c, key.d));
// warm up only.
for (int i = 0; i < 5; i++) {
find("Warm up String", keys, n, stringFinder);
find("Warm up Object", keys, n, objectFinder);
find("Warm up Builder", keys, n, builderFinder);
}
long s = 0;
long b = 0;
long o = 0;
for (int i = 0; i < 10; i++) {
b += find("Builder", keys, n, builderFinder);
o += find("Object", keys, n, objectFinder);
s += find("String", keys, n, stringFinder);
}
System.out.println("\n#################################### ");
System.out.printf("Final Result for Object: %010d \n", o);
System.out.printf("Final Result for Builder: %010d \n", b);
System.out.printf("Final Result for String: %010d \n", s);
System.out.println("\n#################################### ");
System.out.printf("String / Object: %f \n", ( 1.0 * s / o));
System.out.printf("Builder / Object: %f \n", ( 1.0 * b / o));
System.out.printf("String / Builder: %f \n", ( 1.0 * s / b));
}
static long find(String name, List<Key> keys, int n, Finder finder) {
long start = System.nanoTime();
for (int i = 0; i < n; i++) {
Key key = keys.get(n - i - 1);
String value = finder.find(key);
// to make sure values are retrieved as expected.
if (!key.a.equals(value)) {
throw new RuntimeException(key.a); //this should never happen.
}
}
long end = System.nanoTime();
long r = end - start;
System.out.printf("With %s: %d \n", name, r);
return r;
}
static void initialize(Map<String, String> withString, Map<Key, String> withObject, List<Key> keys, int n) {
for (int i = 0; i < n; i++) {
String a = GENERATOR.generate();
String b = GENERATOR.generate();
String c = GENERATOR.generate();
String d = GENERATOR.generate();
Key key = new Key(a, b, c, d);
withString.put((key.a + "#" + key.b + "#" + key.c + "#" + key.d), key.a);
withObject.put(key, key.a);
keys.add(key);
}
}
static final class Key {
final String a;
final String b;
final String c;
final String d;
private int hash = 0;
Key(String a, String b, String c, String d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
@Override
public boolean equals(Object that) {
if (this == that) return true;
if (!(that instanceof Key)) return false;
Key key = (Key) that;
return this.a.equals(key.a)
&& this.b.equals(key.b)
&& this.c.equals(key.c)
&& this.d.equals(key.d);
}
@Override
public int hashCode() {
if (hash == 0) {
hash = 17;
hash += 31 * a.hashCode();
hash += 31 * b.hashCode();
hash += 31 * c.hashCode();
hash += 31 * d.hashCode();
}
return hash;
}
}
@FunctionalInterface
interface Finder { String find(Key key); }
@FunctionalInterface
interface StringGenerator { String generate(); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment