算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法

1.

 1 package algorithms.analysis14;
 2 
 3 import algorithms.util.StdOut;
 4 import algorithms.util.StdRandom;
 5 
 6 /******************************************************************************
 7  *  Compilation:  javac DoublingTest.java
 8  *  Execution:    java DoublingTest
 9  *  Dependencies: ThreeSum.java Stopwatch.java StdRandom.java StdOut.java
10  *
11  *  % java DoublingTest 
12  *      250   0.0
13  *      500   0.0
14  *     1000   0.1
15  *     2000   0.6
16  *     4000   4.5
17  *     8000  35.7
18  *  ...
19  *
20  ******************************************************************************/
21 
22 /**
23  *  The <tt>DoublingTest</tt> class provides a client for measuring
24  *  the running time of a method using a doubling test.
25  *  <p>
26  *  For additional documentation, see <a href="http://algs4.cs.princeton.edu/14analysis">Section 1.4</a>
27  *  of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
28  *
29  *  @author Robert Sedgewick
30  *  @author Kevin Wayne
31  */
32 public class DoublingTest {
33     private static final int MAXIMUM_INTEGER = 1000000;
34 
35     // This class should not be instantiated.
36     private DoublingTest() { }
37 
38     /**
39      * Returns the amount of time to call <tt>ThreeSum.count()</tt> with <em>N</em>
40      * random 6-digit integers.
41      * @param N the number of integers
42      * @return amount of time (in seconds) to call <tt>ThreeSum.count()</tt>
43      *   with <em>N</em> random 6-digit integers
44      */
45     public static double timeTrial(int N) {
46         int[] a = new int[N];
47         for (int i = 0; i < N; i++) {
48             a[i] = StdRandom.uniform(-MAXIMUM_INTEGER, MAXIMUM_INTEGER);
49         }
50         Stopwatch timer = new Stopwatch();
51         ThreeSum.count(a);
52         return timer.elapsedTime();
53     }
54 
55     /**
56      * Prints table of running times to call <tt>ThreeSum.count()</tt>
57      * for arrays of size 250, 500, 1000, 2000, and so forth.
58      */
59     public static void main(String[] args) { 
60         for (int N = 250; true; N += N) {
61             double time = timeTrial(N);
62             StdOut.printf("%7d %5.1f
", N, time);
63         } 
64     } 
65 } 

2.

 1 package algorithms.analysis14;
 2 
 3 import algorithms.util.StdOut;
 4 import algorithms.util.StdRandom;
 5 
 6 /******************************************************************************
 7  *  Compilation:  javac DoublingRatio.java
 8  *  Execution:    java DoublingRatio
 9  *  Dependencies: ThreeSum.java Stopwatch.java StdRandom.java StdOut.java
10  *
11  *
12  *  % java DoublingRatio
13  *      250   0.0    2.7
14  *      500   0.0    4.8
15  *     1000   0.1    6.9
16  *     2000   0.6    7.7
17  *     4000   4.5    8.0
18  *     8000  35.7    8.0
19  *  ...
20  *
21  ******************************************************************************/
22 
23 /**
24  *  The <tt>DoublingRatio</tt> class provides a client for measuring
25  *  the running time of a method using a doubling ratio test.
26  *  <p>
27  *  For additional documentation, see <a href="http://algs4.cs.princeton.edu/14analysis">Section 1.4</a>
28  *  of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
29  *
30  *  @author Robert Sedgewick
31  *  @author Kevin Wayne
32  */
33 public class DoublingRatio {
34     private static final int MAXIMUM_INTEGER = 1000000;
35 
36     // This class should not be instantiated.
37     private DoublingRatio() { }
38 
39     /**
40      * Returns the amount of time to call <tt>ThreeSum.count()</tt> with <em>N</em>
41      * random 6-digit integers.
42      * @param N the number of integers
43      * @return amount of time (in seconds) to call <tt>ThreeSum.count()</tt>
44      *   with <em>N</em> random 6-digit integers
45      */
46     public static double timeTrial(int N) {
47         int[] a = new int[N];
48         for (int i = 0; i < N; i++) {
49             a[i] = StdRandom.uniform(-MAXIMUM_INTEGER, MAXIMUM_INTEGER);
50         }
51         Stopwatch timer = new Stopwatch();
52         ThreeSum.count(a);
53         return timer.elapsedTime();
54     }
55 
56     /**
57      * Prints table of running times to call <tt>ThreeSum.count()</tt>
58      * for arrays of size 250, 500, 1000, 2000, and so forth, along
59      * with ratios of running times between successive array sizes.
60      */
61     public static void main(String[] args) { 
62         double prev = timeTrial(125);
63         for (int N = 250; true; N += N) {
64             double time = timeTrial(N);
65             StdOut.printf("%6d %7.1f %5.1f
", N, time, time/prev);
66             prev = time;
67         } 
68     } 
69 } 
原文地址:https://www.cnblogs.com/shamgod/p/5413797.html