public static void main(String[] args) { int n = Integer.parseInt(args[0]); int t = Integer.parseInt(args[1]); Stopwatch sw = new Stopwatch(); PercolationStats ps = new PercolationStats(n, t); double secondsElapsed = sw.elapsedTime(); StdOut.println("Seconds elapsed = " + secondsElapsed); StdOut.println("mean = " + ps.mean()); StdOut.println("stddev = " + ps.stddev()); StdOut.println("95% confidence interval = " + ps.confidenceLo() + ", " + ps.confidenceHi()); }
public static void main(String[] args) { Comparable[] array = new Comparable[100000]; Random r = new Random(); for (int i = 0; i < array.length; i++) { array[i] = r.nextInt(10000); } Arrays.sort(array); Stopwatch stopwatch = new Stopwatch(); EditedQuick.sort(array); System.out.println(stopwatch.elapsedTime()); }
public static double TrialQuickUnionUF(int N) { Stopwatch watch = new Stopwatch(); QuickUnionUF uf = new QuickUnionUF(N); for (int i = 0; i < N; i++) { int p = StdRandom.uniform(N); int q = StdRandom.uniform(N); if (!uf.connected(p, q)) { uf.union(p, q); } } double time = watch.elapsedTime(); // printTree(uf); return time; }
// if you go to larger n, then you will run out of Java Heap space public static void main(String[] args) { double[][] results = new double[12][8]; double time; for (int n = 4, i = 0; n <= 8192; n *= 2, i++) { // Create two bags of the integers [1, n]. Bag 'one' has all numbers // while bag two has odd numbers only. // Add the numbers in REVERSE ORDER otherwise this will take a LOOOOONG time. UniqueBag<Integer> one = new UniqueBag<Integer>(); UniqueBag<Integer> two = new UniqueBag<Integer>(); UniqueBag<Integer> three = new UniqueBag<Integer>(); for (int k = n; k >= 1; k--) { one.add(k); if (k % 2 == 1) { two.add(k); } } // at this point you can benchmark some operations: // size, identical, add, toArray, contains, remove, intersects, union // use StopWatch as you did before. Stopwatch sizeStop = new Stopwatch(); one.size(); time = sizeStop.elapsedTime(); results[i][0] = time; Stopwatch identicalStop = new Stopwatch(); one.identical(two); time = identicalStop.elapsedTime(); results[i][1] = time; Stopwatch addStop = new Stopwatch(); one.add(n + 1); time = addStop.elapsedTime(); results[i][2] = time; Stopwatch arrayStop = new Stopwatch(); one.toArray(); time = arrayStop.elapsedTime(); results[i][3] = time; Stopwatch containsStop = new Stopwatch(); one.contains(1); time = containsStop.elapsedTime(); results[i][4] = time; Stopwatch removeStop = new Stopwatch(); one.remove(1); time = removeStop.elapsedTime(); results[i][5] = time; Stopwatch intersectsStop = new Stopwatch(); one.intersects(two); time = intersectsStop.elapsedTime(); results[i][6] = time; Stopwatch unionStop = new Stopwatch(); one.union(two); time = unionStop.elapsedTime(); results[i][7] = time; } StdOut.println("Time Trials"); StdOut.println("Trial\tsize\tidentical\tadd\ttoArray\tcontains\tremove\tintersects\tunion\n"); for (int i = 0; i < 12; i++) StdOut.println( i + "\t" + results[i][0] + "\t" + results[i][1] + "\t" + results[i][2] + "\t" + results[i][3] + "\t" + results[i][4] + "\t" + results[i][5] + "\t" + results[i][6] + "\t" + results[i][7]); }