Skip to content

Instantly share code, notes, and snippets.

Created April 30, 2012 08:11
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 anonymous/2556469 to your computer and use it in GitHub Desktop.
Save anonymous/2556469 to your computer and use it in GitHub Desktop.
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