CSC300: Using a timer [20/21] Previous pageContentsNext page

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package algs11;
import stdlib.*;
public class PlaygroundMax {
  public static double max (double[] a) {
    if (a.length == 0) { throw new IllegalArgumentException(); }
    double m = a[0];
    int i = 1;
    while (i < a.length) {
      if (m < a[i]) { m = a[i]; }
      i += 1;
    }
    return m;
  }

  public static double timeTrial(int N) {
    double[] a = ArrayGenerator.doubleSortedUnique(N);
    Stopwatch s = new Stopwatch();
    max (a);
    return s.elapsedTime();
  }

  private static final int MIN =         1_000;
  private static final int MAX = 1_000_000_000;
  public static void main(String[] args) {
    double prev = timeTrial(MIN);
    for (int N = MIN*2; N<=MAX; N += N) {
      double time = timeTrial(N);
      StdOut.format("%10d %9.3f %5.1f\n", N, time, time/prev);
      prev = time;
    }
  }
}

Previous pageContentsNext page