001package algs11; 002import stdlib.*; 003public class PlaygroundPerformance { 004 /* Return number of times 5.0 occurs in the list */ 005 public static int numFives (double[] a) { 006 int result = 0; 007 for (int i=0; i<a.length; i++) 008 if (a[i] == 5.0) 009 result++; 010 return result; 011 } 012 public static double timeTrial(int N) { 013 double[] a = ArrayGenerator.doubleRandom(N, 10); 014 Stopwatch s = new Stopwatch(); 015 numFives(a); 016 return s.elapsedTime(); 017 } 018 019 private static final int MIN = 1_000_000; 020 private static final int MAX = 1_000_000_000; 021 public static void main(String[] args) { 022 double prev = timeTrial(MIN); 023 for (int N = MIN*2; N<=MAX; N += N) { 024 double time = timeTrial(N); 025 StdOut.format("%,13d %10.3f %10.3f\n", N, time, time/prev); 026 prev = time; 027 } 028 } 029}