// CSE 143, Winter 2010, Marty Stepp // This client creates various sets and times how quickly they can add and search // for elements. // // On my machine: // HashSet timing test... 10806 ms // java.util.TreeSet timing test... 2614 ms // HashSet timing test... 3074 ms // // (Our hash set is slower than Java's because we create so many LinkedList objects. // Java's manually manipulates node objects in more efficient ways. // But our hash set is still very efficient overall.) import java.util.*; public class TimingTest { private static final int SIZE = 400000; public static void main(String[] args) { // timing tests for collections we wrote timingTest(new HashSet()); // timing tests on built-in Java collections from java.util timingTest(new TreeSet()); timingTest(new HashSet()); // timingTest(new ArrayList()); // too slow! } // 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(HashSet 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(Collection 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"); } }