// CSE 143, Winter 2009, Marty Stepp // This client creates various sets and times how quickly they can add and search // for elements. It compares our TreeAnythingSet (descendant of IntTree) and // our new hash set against Java's built-in TreeSet and HashSet. // // On my machine: // TreeAnythingSet timing test... 2433 ms // HashIntSet timing test... 1282 ms // HashAnythingSet timing test... 1492 ms // java.util.TreeSet timing test... 1883 ms // java.util.HashSet timing test... 551 ms import java.util.*; public class UseSet { private static final int SIZE = 400000; public static void main(String[] args) { // construct a binary search tree treeTests(); // timing tests for collections we wrote timingTest(new TreeAnythingSet()); timingTest(new HashIntSet()); timingTest(new HashAnythingSet(SIZE * 3 / 2)); // timing tests on built-in Java collections from java.util timingTest(new TreeSet()); timingTest(new HashSet(SIZE * 3 / 2)); } // A timing test method that adds lots of elements to the given set and // times how long this takes. Useful for comparing set implementations. public static void timingTest(HashIntSet set) { System.out.print(set.getClass().getName() + " timing test... "); long startTime = System.currentTimeMillis(); for (int i = 0; i < SIZE; i++) { // add lots of various elements and search for random things set.add((int) (Math.random() * 100000000)); set.contains((int) (Math.random() * 100000000)); } long endTime = System.currentTimeMillis(); System.out.println((endTime - startTime) + " ms"); } // A timing test method that adds lots of elements to the given set and // times how long this takes. Useful for comparing set implementations. public static void timingTest(AnythingSet set) { System.out.print(set.getClass().getName() + " timing test... "); long startTime = System.currentTimeMillis(); for (int i = 0; i < SIZE; i++) { // add lots of various elements and search for random things set.add((int) (Math.random() * 100000000)); set.contains((int) (Math.random() * 100000000)); } long endTime = System.currentTimeMillis(); System.out.println((endTime - startTime) + " ms"); } // Version of timing test that works on built-in Java collections. public static void timingTest(Set set) { System.out.print(set.getClass().getName() + " timing test... "); long startTime = System.currentTimeMillis(); for (int i = 0; i < SIZE; i++) { // add lots of various elements and search for random things set.add((int) (Math.random() * 100000000)); set.contains((int) (Math.random() * 100000000)); } long endTime = System.currentTimeMillis(); System.out.println((endTime - startTime) + " ms"); } // old code that just constructs and tests trees. Not very interesting. public static void treeTests() { TreeAnythingSet tree = new TreeAnythingSet(); tree.add(9); tree.add(6); tree.add(7); tree.add(14); tree.add(11); tree.add(19); System.out.println("The tree:"); tree.printSideways(); System.out.println("contains tests:"); System.out.println(tree.contains(9)); System.out.println(tree.contains(14)); System.out.println(tree.contains(11)); System.out.println(tree.contains(7)); System.out.println(tree.contains(-1)); System.out.println(tree.contains(0)); System.out.println(tree.contains(42)); System.out.println(tree.contains(10)); TreeAnythingSet tree2 = new TreeAnythingSet(); tree2.add("Marty"); tree2.add("Dan"); tree2.add("Lisa"); tree2.add("Kim"); tree2.add("Stuart"); tree2.printSideways(); } }