Below is the syntax highlighted version of DoublingTest.java
from §1.4 Analysis of Algorithms.
/************************************************************************* * Compilation: javac DoublingTest.java * Execution: java DoublingTest * Dependencies: ThreeSum.java Stopwatch.java StdRandom.java StdOut.java * * % java DoublingTest * 512 6.48 * 1024 8.30 * 2048 7.75 * 4096 8.00 * 8192 8.05 * ... * *************************************************************************/ public class DoublingTest { public static double timeTrial(int N) { int MAX = 1000000; int[] a = new int[N]; for (int i = 0; i < N; i++) { a[i] = StdRandom.uniform(-MAX, MAX); } Stopwatch s = new Stopwatch(); int cnt = ThreeSum.count(a); return s.elapsedTime(); } public static void main(String[] args) { for (int N = 250; true; N += N) { double time = timeTrial(N); StdOut.printf("%7d %5.1f\n", N, time); } } }