Create a gist now

Instantly share code, notes, and snippets.

anonymous /gist:2556469
Created Apr 30, 2012

Embed
Set Tester
import com.google.common.base.Stopwatch;
import java.util.*;
public class SetTester {
List<String> data = new ArrayList<String>();
List<String> shuffled;
int COUNT = 1000000;
public SetTester() {
for (int i = 0; i < COUNT; i++) {
data.add(String.valueOf(Math.random()));
}
shuffled = new ArrayList<String>(data);
Collections.shuffle(shuffled);
}
public void test(String testStr, Set<String> set) {
System.out.println(testStr);
Stopwatch stopWatch = new Stopwatch().start();
for (String s : data) {
set.add(s);
}
System.out.println("Insert:" + stopWatch.elapsedMillis());
stopWatch.reset().start();
int i = 0;
for (String s : shuffled) {
if (set.contains(s))
i++;
}
System.out.println("Query:" + stopWatch.elapsedMillis());
assert i == COUNT;
stopWatch.reset().start();
for (String s : shuffled) {
set.remove(s);
}
System.out.println("Remove:" + stopWatch.elapsedMillis());
assert set.size() == 0;
}
public static void main(String[] args) {
SetTester tester = new SetTester();
for (int i = 0; i < 3; i++) {
System.out.println("Test:" + i);
tester.test("HashSet", new HashSet<String>());
tester.test("LinkedHashSet", new LinkedHashSet<String>());
tester.test("TreeSet", new TreeSet<String>());
System.out.println("---------------");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment