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