Skip to content

Instantly share code, notes, and snippets.

@mdakin
Created May 2, 2012 01:32
Show Gist options
  • Save mdakin/2572904 to your computer and use it in GitHub Desktop.
Save mdakin/2572904 to your computer and use it in GitHub Desktop.
Java hashset perf test
private static void runTests(HashSet<String> se, List<String> data) {
Stopwatch stopWatch = new Stopwatch().start();
for (String s : data) {
se.add(s);
}
System.out.println("Add:" + stopWatch.elapsedMillis() + " ms.");
int i = 0;
stopWatch.reset().start();
for (String s : data) {
if (se.contains(s)) {
i+= s.length();
}
}
System.out.println("Contains:" + stopWatch.elapsedMillis() + " ms.");
stopWatch.reset().start();
for (String s : se) {
i+= s.length();
}
System.out.println("Iterate: " + stopWatch.elapsedMillis() + " ms.");
stopWatch.reset().start();
for (String s : data) {
se.remove(s);
}
System.out.println("Remove: " + stopWatch.elapsedMillis() + " ms.");
stopWatch.reset().start();
Collections.sort(data);
System.out.println("Sort list: " + stopWatch.elapsedMillis() + " ms.");
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
List<String> data = new ArrayList<String>();
for (int i=0; i<500000; i++) {
String s = String.valueOf(Math.random());
data.add(s);
}
runTests(new HashSet<String>(), data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment