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
33
34
35
36
37
38
39
40
41
package algs14;
import stdlib.*;
public class XPerformanceOfStrings {
  /** create a string consisting of N asterisks */
  public static String makeStringUsingConcat (int N) {
    String result = "";
    for (int i=0; i<N; i++) 
      result = result + "*";
    return result;
  }
  /** create a string consisting of N asterisks */
  public static String makeStringUsingBuffer (int N) {
    StringBuffer result = new StringBuffer ();
    for (int i=0; i<N; i++) 
      result.append ("*");
    return result.toString ();
  }
  public static double timeTrial(int N) {
    int T = 1; // number of tests
    double sum = 0;
    for (int t = 0; t < T; t++) {
      Stopwatch s = new Stopwatch();
      //makeStringUsingConcat (N);
      makeStringUsingBuffer (N);
      sum +=  s.elapsedTime();
    }
    return sum/T;
  }

  private static final int MIN = 8000;
  private static final int MAX = 32768000;
  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;
    }
  }
}