001package algs13;
002import stdlib.*;
003import com.javamex.classmexer.MemoryUtil;
004
005/* ***********************************************************************
006 *  Compilation:  javac -cp .:classmexer.jar XMemoryOfStacks.java
007 *  Execution:    java -cp  .:classmexer.jar -javaagent:classmexer.jar XMemoryOfStacks
008 *
009 *  Note: the Integer values -128 to 127 are cached once created.
010 *
011 *  % java -cp .:classmexer.jar -javaagent:classmexer.jar XMemoryOfStacks 10
012 *  size of Integer  = 24
013 *  Stack of N null references   = 40.00 N + 32.00  (R^2 = 1.000)
014 *  Stack<Integer> of size N     = 64.00 N + 32.00  (R^2 = 1.000)
015 *  StackOfInts of size N        = 32.00 N + 32.00  (R^2 = 1.000)
016 *  StackOfIntegers of size N    = 56.00 N + 32.00  (R^2 = 1.000)
017 *  Queue of N null references   = 40.00 N + 40.00  (R^2 = 1.000)
018 *  Bag of N null references     = 40.00 N + 32.00  (R^2 = 1.000)
019 *
020 *************************************************************************/
021
022public class XMemoryOfStacks {
023
024        public static void nullStack() {
025                int[] sizes = { 64, 128, 192, 256, 320, 384, 448, 512, 576,
026                                640, 704, 768, 832, 896, 960, 1024 };
027                int M = sizes.length;
028
029                double[] x = new double[M];
030                double[] memory = new double[M];
031
032                for (int i = 0; i < M; i++) {
033                        int N = sizes[i];
034                        Stack<Integer> stack = new Stack<>();
035                        for (int j = 0; j < N; j++)
036                                stack.push(null);
037                        x[i] = stack.size();
038                        memory[i] = MemoryUtil.deepMemoryUsageOf(stack);
039                }
040
041                XLinearRegression regression = new XLinearRegression(x, memory);
042                StdOut.println("Stack of N null references   = " + regression);
043        }
044
045        public static void IntegerStack() {
046                int[] sizes = { 64, 128, 192, 256, 320, 384, 448, 512, 576,
047                                640, 704, 768, 832, 896, 960, 1024 };
048                int M = sizes.length;
049
050                double[] x = new double[M];
051                double[] memory = new double[M];
052
053                for (int i = 0; i < M; i++) {
054                        int N = sizes[i];
055                        Stack<Integer> stack = new Stack<>();
056                        for (int j = 0; j < N; j++)
057                                stack.push(128 + StdRandom.uniform(100000));
058                        x[i] = stack.size();
059                        memory[i] = MemoryUtil.deepMemoryUsageOf(stack);
060                }
061
062                XLinearRegression regression = new XLinearRegression(x, memory);
063                StdOut.println("Stack<Integer> of size N     = " + regression);
064        }
065
066        public static void StackOfInts() {
067                int[] sizes = { 64, 128, 192, 256, 320, 384, 448, 512, 576,
068                                640, 704, 768, 832, 896, 960, 1024 };
069                int M = sizes.length;
070
071                double[] x = new double[M];
072                double[] memory = new double[M];
073
074                for (int i = 0; i < M; i++) {
075                        int N = sizes[i];
076                        Stack<Integer> stack = new Stack<>();
077                        for (int j = 0; j < N; j++)
078                                stack.push(128 + StdRandom.uniform(100000));
079                        x[i] = stack.size();
080                        memory[i] = MemoryUtil.deepMemoryUsageOf(stack);
081                }
082
083                XLinearRegression regression = new XLinearRegression(x, memory);
084                StdOut.println("StackOfInts of size N        = " + regression);
085        }
086
087
088        public static void StackOfIntegers() {
089                int[] sizes = { 64, 128, 192, 256, 320, 384, 448, 512, 576,
090                                640, 704, 768, 832, 896, 960, 1024 };
091                int M = sizes.length;
092
093                double[] x = new double[M];
094                double[] memory = new double[M];
095
096                for (int i = 0; i < M; i++) {
097                        int N = sizes[i];
098                        Stack<Integer> stack = new Stack<>();
099                        for (int j = 0; j < N; j++)
100                                stack.push(128 + StdRandom.uniform(100000));
101                        x[i] = stack.size();
102                        memory[i] = MemoryUtil.deepMemoryUsageOf(stack);
103                }
104
105                XLinearRegression regression = new XLinearRegression(x, memory);
106                StdOut.println("StackOfIntegers of size N    = " + regression);
107        }
108
109        public static void nullQueue() {
110                int[] sizes = { 64, 128, 192, 256, 320, 384, 448, 512, 576,
111                                640, 704, 768, 832, 896, 960, 1024 };
112                int M = sizes.length;
113
114                double[] x = new double[M];
115                double[] memory = new double[M];
116
117                for (int i = 0; i < M; i++) {
118                        int N = sizes[i];
119                        Queue<Integer> queue = new Queue<>();
120                        for (int j = 0; j < N; j++)
121                                queue.enqueue(null);
122                        x[i] = queue.size();
123                        memory[i] = MemoryUtil.deepMemoryUsageOf(queue);
124                }
125
126                XLinearRegression regression = new XLinearRegression(x, memory);
127                StdOut.println("Queue of N null references   = " + regression);
128        }
129
130        public static void nullBag() {
131                int[] sizes = { 64, 128, 192, 256, 320, 384, 448, 512, 576,
132                                640, 704, 768, 832, 896, 960, 1024 };
133                int M = sizes.length;
134
135                double[] x = new double[M];
136                double[] memory = new double[M];
137
138                for (int i = 0; i < M; i++) {
139                        int N = sizes[i];
140                        Bag<Integer> bag = new Bag<>();
141                        for (int j = 0; j < N; j++)
142                                bag.add(null);
143                        x[i] = bag.size();
144                        memory[i] = MemoryUtil.deepMemoryUsageOf(bag);
145                }
146
147                XLinearRegression regression = new XLinearRegression(x, memory);
148                StdOut.println("Bag of N null references     = " + regression);
149        }
150
151
152        public static void main(String[] args) {
153                Integer x = Integer.valueOf(123456);
154                StdOut.println("size of Integer  = " + MemoryUtil.memoryUsageOf(x));
155
156                nullStack();
157                IntegerStack();
158                StackOfInts();
159                StackOfIntegers();
160                nullQueue();
161                nullBag();
162                /*
163        resizingNullStack();
164        resizingIntStack();
165                 */
166        }
167
168}