001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package algs14;
import stdlib.*;
import java.util.LinkedList;
import com.javamex.classmexer.MemoryUtil;

/* ***********************************************************************
 *  Compilation:  javac -cp .:classmexer.jar XMemoryOfStrings.java
 *  Execution:    java  -cp .:classmexer.jar -javaagent:classmexer.jar XMemoryOfStrings
 *  Dependencies: StdOut.java StdRandom.java classmexer.jar
 *
 *  In eclipse, add
 *    -javaagent:classmexer.jar
 *  under Run -> Run Configurations... -> Arguments -> VM arguments
 *
 *  % java -cp .:classmexer.jar -javaagent:classmexer.jar XMemoryOfStrings
 *  String of length N        = 2.00 N + 64.00  (R^2 = 1.000)
 *  char[] array of length N  = 2.00 N + 24.00  (R^2 = 1.000)
 *  String[] of N suffixes    = 50.00 N + 48.00  (R^2 = 1.000)
 *  memory of genome = "CGCCTGGCGTCTGTAC"    = 96
 *  memory of codon = genome.substring(6, 9) = 96
 *  total memory of genome and codon         = 136
 *
 *************************************************************************/

public class XMemoryOfStrings {

  // random string of length N consisting of only uppercase letters
  public static String random(int N) {
    if (N == 0) return "";
    if (N == 1) {
      char c = (char) ('A' + StdRandom.uniform(26));
      return c + "";
    }
    return random(N/2) + random(N - N/2);
  }


  // string of given length
  public static void string() {
    int[] sizes = {
        64, 128, 192, 256, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
    };
    int M = sizes.length;

    double[] x = new double[M];
    double[] memory = new double[M];

    for (int i = 0; i < M; i++) {
      String s = random(sizes[i]);
      x[i] = s.length();
      memory[i] = MemoryUtil.deepMemoryUsageOf(s);
    }

    XLinearRegression regression = new XLinearRegression(x, memory);
    StdOut.println("String of length N        = " + regression);
  }

  // character array
  public static void charArray() {
    int[] sizes = {
        64, 128, 192, 256, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
    };
    int M = sizes.length;

    double[] x = new double[M];
    double[] memory = new double[M];

    for (int i = 0; i < M; i++) {
      char[] s = random(sizes[i]).toCharArray();
      x[i] = s.length;
      memory[i] = MemoryUtil.deepMemoryUsageOf(s);
    }

    XLinearRegression regression = new XLinearRegression(x, memory);
    StdOut.println("char[] array of length N  = " + regression);
  }

  // array of suffixes
  public static void suffixes() {
    int[] sizes = {
        64, 128, 192, 256, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
    };
    int M = sizes.length;

    double[] x = new double[M];
    double[] memory = new double[M];

    for (int i = 0; i < M; i++) {
      String s = random(sizes[i]);
      String[] suffixes = new String[s.length()];
      for (int j = 0; j < s.length(); j++)
        suffixes[j] = s.substring(j);
      x[i] = s.length();
      memory[i] = MemoryUtil.deepMemoryUsageOf(suffixes);
    }

    XLinearRegression regression = new XLinearRegression(x, memory);
    StdOut.println("String[] of N suffixes    = " + regression);
  }

  // substring
  public static void substring() {
    String genome = "CGCCTGGCGTCTGTAC";
    String codon = genome.substring(6, 9);
    LinkedList<String> list = new LinkedList<>();
    list.add(genome);
    list.add(codon);

    StdOut.println("shallow memory of genome = \"CGCCTGGCGTCTGTAC\"    = " + MemoryUtil.memoryUsageOf(genome));
    StdOut.println("shallow memory of codon = genome.substring(6, 9) = " + MemoryUtil.memoryUsageOf(codon));
    StdOut.println("shallow memory of list                           = " + MemoryUtil.memoryUsageOf(list));

    StdOut.println("deep memory of genome = \"CGCCTGGCGTCTGTAC\"    = " + MemoryUtil.deepMemoryUsageOf(genome));
    StdOut.println("deep memory of codon = genome.substring(6, 9) = " + MemoryUtil.deepMemoryUsageOf(codon));
    StdOut.println("deep memory of both                           = " + MemoryUtil.deepMemoryUsageOfAll(list));
  }

  public static void main(String[] args) {
    //string();
    //charArray();
    //suffixes();
    substring();
  }
}