001package algs14;
002import stdlib.*;
003import algs12.Date;
004import com.javamex.classmexer.MemoryUtil;
005/* ***********************************************************************
006 *  Compilation:  javac -cp .:classmexer.jar:jama.jar XMemoryOfArrays.java
007 *  Execution:    java  -cp .:classmexer.jar -javaagent:classmexer.jar:jama.jar XMemoryOfArrays
008 *  Dependencies: StdOut.java StdRandom.java classmexer.jar jama.jar
009 *                LinearRegression.java MultipleLinearRegresssion.java
010 *
011 *  In eclipse, add
012 *    -javaagent:classmexer.jar
013 *  under Run -> Run Configurations... -> Arguments -> VM arguments
014 *
015 *  % java -cp .:classmexer.jar:jama.jar -javaagent:classmexer.jar XMemoryOfArrays
016 *  boolean[] array of length N  = 1.00 N + 24.00  (R^2 = 1.000)
017 *  char[]    array of length N  = 2.00 N + 24.00  (R^2 = 1.000)
018 *  int[]     array of length N  = 4.00 N + 24.00  (R^2 = 1.000)
019 *  double[]  array of length N  = 8.00 N + 24.00  (R^2 = 1.000)
020 *  array of N null references   = 8.00 N + 24.00  (R^2 = 1.000)
021 *  Date                         = 32
022 *  Date[] array of length N     = 40.00 N + 24.00  (R^2 = 1.000)
023 *  M-by-N int[][] array         = 24.00 + 32.00 M + 0.00 N + 4.00 MN bytes (R^2 = 1.000)
024 *  M-by-N double[][] array      = 24.00 + 32.00 M + 0.00 N + 8.00 MN bytes (R^2 = 1.000)
025 *
026 *************************************************************************/
027
028public class XMemoryOfArrays {
029
030        // boolean array
031        public static void booleanArray() {
032                int[] sizes = {
033                                64, 128, 192, 256, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
034                };
035                int M = sizes.length;
036
037                double[] x = new double[M];
038                double[] memory = new double[M];
039
040                for (int i = 0; i < M; i++) {
041                        int N = sizes[i];
042                        boolean[] a = new boolean[N];
043                        x[i] = a.length;
044                        memory[i] = MemoryUtil.deepMemoryUsageOf(a);
045                }
046
047                XLinearRegression regression = new XLinearRegression(x, memory);
048                StdOut.println("boolean[] array of length N  = " + regression);
049        }
050
051        // character array
052        public static void charArray() {
053                int[] sizes = {
054                                64, 128, 192, 256, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
055                };
056                int M = sizes.length;
057
058                double[] x = new double[M];
059                double[] memory = new double[M];
060
061                for (int i = 0; i < M; i++) {
062                        int N = sizes[i];
063                        char[] a = new char[N];
064                        x[i] = a.length;
065                        memory[i] = MemoryUtil.deepMemoryUsageOf(a);
066                }
067
068                XLinearRegression regression = new XLinearRegression(x, memory);
069                StdOut.println("char[]    array of length N  = " + regression);
070        }
071
072        // integer array
073        public static void intArray() {
074                int[] sizes = {
075                                64, 128, 192, 256, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
076                };
077                int M = sizes.length;
078
079                double[] x = new double[M];
080                double[] memory = new double[M];
081
082                for (int i = 0; i < M; i++) {
083                        int N = sizes[i];
084                        int[] a = new int[N];
085                        x[i] = a.length;
086                        memory[i] = MemoryUtil.deepMemoryUsageOf(a);
087                }
088
089                XLinearRegression regression = new XLinearRegression(x, memory);
090                StdOut.println("int[]     array of length N  = " + regression);
091        }
092
093        // double array
094        public static void doubleArray() {
095                int[] sizes = {
096                                64, 128, 192, 256, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
097                };
098                int M = sizes.length;
099
100                double[] x = new double[M];
101                double[] memory = new double[M];
102
103                for (int i = 0; i < M; i++) {
104                        int N = sizes[i];
105                        double[] a = new double[N];
106                        x[i] = a.length;
107                        memory[i] = MemoryUtil.deepMemoryUsageOf(a);
108                }
109
110                XLinearRegression regression = new XLinearRegression(x, memory);
111                StdOut.println("double[]  array of length N  = " + regression);
112        }
113
114        // Integer array of null
115        public static void nullArray() {
116                int[] sizes = {
117                                64, 128, 192, 256, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
118                };
119                int M = sizes.length;
120
121                double[] x = new double[M];
122                double[] memory = new double[M];
123
124                for (int i = 0; i < M; i++) {
125                        int N = sizes[i];
126                        Date[] a = new Date[N];
127                        x[i] = a.length;
128                        memory[i] = MemoryUtil.deepMemoryUsageOf(a);
129                }
130
131                XLinearRegression regression = new XLinearRegression(x, memory);
132                StdOut.println("array of N null references   = " + regression);
133        }
134
135        // Integer array
136        public static void DateArray() {
137                int[] sizes = {
138                                64, 128, 192, 256, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
139                };
140                int M = sizes.length;
141
142                double[] x = new double[M];
143                double[] memory = new double[M];
144
145                for (int i = 0; i < M; i++) {
146                        int N = sizes[i];
147                        Date[] a = new Date[N];
148                        for (int j = 0; j < N; j++) {
149                                int month = 1 + StdRandom.uniform(12);
150                                int day   = 1 + StdRandom.uniform(28);
151                                int year  = 1900 + StdRandom.uniform(100);
152                                a[j] = new Date(month, day, year);
153                        }
154                        x[i] = a.length;
155                        memory[i] = MemoryUtil.deepMemoryUsageOf(a);
156                }
157
158                Date date = new Date(12, 31, 1999);
159                StdOut.println("Date                         = " + MemoryUtil.deepMemoryUsageOf(date));
160                XLinearRegression regression = new XLinearRegression(x, memory);
161                StdOut.println("Date[] array of length N     = " + regression);
162        }
163
164
165        // int[][] array
166        public static void int2dArray() {
167                int[] sizes1 = {
168                                64, 128, 192, 256, 320, 384, 448, 512, 576,
169                                640, 704, 768, 832, 896, 960, 1024
170                };
171
172                int[] sizes2 = {
173                                64, 64, 64, 384, 384, 384, 96, 96, 96,
174                                16, 8, 128, 32, 16, 24, 24
175                };
176
177                double[][] x = new double[sizes1.length][4];
178                double[] memory = new double[sizes1.length];
179
180                for (int i = 0; i < sizes1.length; i++) {
181                        int M = sizes1[i];
182                        int N = sizes2[i];
183                        int[][] a = new int[M][N];
184                        x[i][0] = 1.0;
185                        x[i][1] = M;
186                        x[i][2] = N;
187                        x[i][3] = M*N;
188                        memory[i] = MemoryUtil.deepMemoryUsageOf(a);
189                }
190
191                XMultipleLinearRegression regression = new XMultipleLinearRegression(x, memory);
192                StdOut.print("M-by-N int[][] array         = ");
193                StdOut.format("%.2f + %.2f M + %.2f N + %.2f MN bytes (R^2 = %.3f)\n",
194                                regression.beta(0), regression.beta(1), regression.beta(2), regression.beta(3), regression.R2());
195        }
196
197        // double[][] array
198        public static void double2dArray() {
199                int[] sizes1 = {
200                                64, 128, 192, 256, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
201                };
202
203                int[] sizes2 = {
204                                64, 64, 64, 384, 384, 384, 96, 96, 96, 16, 8, 128, 32, 16, 24, 24
205                };
206
207                double[][] x = new double[sizes1.length][4];
208                double[] memory = new double[sizes1.length];
209
210                for (int i = 0; i < sizes1.length; i++) {
211                        int M = sizes1[i];
212                        int N = sizes2[i];
213                        double[][] a = new double[M][N];
214                        x[i][0] = 1.0;
215                        x[i][1] = M;
216                        x[i][2] = N;
217                        x[i][3] = M*N;
218                        memory[i] = MemoryUtil.deepMemoryUsageOf(a);
219                }
220
221                XMultipleLinearRegression regression = new XMultipleLinearRegression(x, memory);
222                StdOut.print("M-by-N double[][] array      = ");
223                StdOut.format("%.2f + %.2f M + %.2f N + %.2f MN bytes (R^2 = %.3f)\n",
224                                regression.beta(0), regression.beta(1), regression.beta(2), regression.beta(3), regression.R2());
225        }
226
227
228        public static void main(String[] args) {
229                booleanArray();
230                charArray();
231                intArray();
232                doubleArray();
233                nullArray();
234                DateArray();
235                int2dArray();
236                double2dArray();
237        }
238}