1 /*
  2  * Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 package org.openjdk.bench.valhalla.sandbox.corelibs;
 25 
 26 import org.openjdk.jmh.annotations.BenchmarkMode;
 27 import org.openjdk.jmh.annotations.Fork;
 28 import org.openjdk.jmh.annotations.Measurement;
 29 import org.openjdk.jmh.annotations.Mode;
 30 import org.openjdk.jmh.annotations.OutputTimeUnit;
 31 import org.openjdk.jmh.annotations.Param;
 32 import org.openjdk.jmh.annotations.Scope;
 33 import org.openjdk.jmh.annotations.State;
 34 import org.openjdk.jmh.annotations.Warmup;
 35 import org.openjdk.jmh.annotations.Benchmark;
 36 import org.openjdk.jmh.annotations.Setup;
 37 import org.openjdk.jmh.annotations.TearDown;
 38 
 39 import org.openjdk.jmh.infra.Blackhole;
 40 
 41 import java.util.ArrayList;
 42 import java.util.Random;
 43 import java.util.concurrent.TimeUnit;
 44 
 45 /**
 46  * Measure performance of List of Integer operations.
 47  * - Set all of int from a set of random numbers (with a seed)
 48  * - Get all
 49  * - Shuffle the array
 50  * - Sort the array
 51  */
 52 
 53 
 54 @Fork(value = 3, jvmArgsAppend = "--enable-preview")
 55 @Warmup(iterations = 5, time = 2)
 56 @Measurement(iterations = 5, time = 3)
 57 @OutputTimeUnit(TimeUnit.MICROSECONDS)
 58 @BenchmarkMode(Mode.AverageTime)
 59 @State(Scope.Thread)
 60 public class ArrayListOfIntBench {
 61 
 62     @Param({
 63             "100",
 64             "1000000",
 65     })
 66     public int size;
 67 
 68     ArrayListInt arrayListInt;
 69     ArrayListPrimitiveInt arrayListPrimitiveInt;
 70     ArrayList<Integer> arrayListOfInteger;
 71     ArrayList<PrimitiveInt> arrayListOfPrimitiveInt;
 72     Random random;
 73 
 74     @Setup
 75     public void setup() {
 76         arrayListInt = new ArrayListInt(size);
 77         for (int i = 0; i < size; i++) {
 78             arrayListInt.add(i, i);
 79         }
 80 
 81         arrayListPrimitiveInt = new ArrayListPrimitiveInt(size);
 82         for (int i = 0; i < size; i++) {
 83             arrayListPrimitiveInt.add(i, new PrimitiveInt(i));
 84         }
 85 
 86         arrayListOfInteger = new ArrayList<>(size);
 87         for (int i = 0; i < size; i++) {
 88             arrayListOfInteger.add(i, i);
 89         }
 90 
 91         arrayListOfPrimitiveInt = new ArrayList<PrimitiveInt>(size);
 92         for (int i = 0; i < size; i++) {
 93             arrayListOfPrimitiveInt.add(i, new PrimitiveInt(i));
 94         }
 95 
 96         random = new Random(42);
 97     }
 98 
 99     @Benchmark
100     public Object appendListInt() {
101         ArrayListInt list = new ArrayListInt(size);
102         for (int i = 0; i < size; i++) {
103             list.add(i);
104         }
105         return list;
106     }
107 
108     @Benchmark
109     public Object appendListPrimitiveInt() {
110         ArrayListPrimitiveInt list = new ArrayListPrimitiveInt(size);
111         for (int i = 0; i < size; i++) {
112             list.add(new PrimitiveInt(i));
113         }
114         return list;
115     }
116 
117     @Benchmark
118     public Object appendListOfInteger() {
119         ArrayList<Integer> list = new ArrayList<>(size);
120         for (int i = 0; i < size; i++) {
121             list.add(i);
122         }
123         return list;
124     }
125 
126     @Benchmark
127     public Object appendListOfPrimitiveInt() {
128         ArrayList<PrimitiveInt> list = new ArrayList<>(size);
129         for (int i = 0; i < size; i++) {
130             list.add(new PrimitiveInt(i));
131         }
132         return list;
133     }
134 
135 
136     @Benchmark
137     public int sumListInt() {
138         int sum = 0;
139         for (int i = 0; i < size; i++) {
140             sum += arrayListInt.get(i);
141         }
142         return sum;
143     }
144 
145     @Benchmark
146     public int sumListOfInteger() {
147         int sum = 0;
148         for (int i = 0; i < size; i++) {
149             sum += arrayListOfInteger.get(i);
150         }
151         return sum;
152     }
153 
154 
155     @Benchmark
156     public int sumListPrimitiveInt() {
157         int sum = 0;
158         for (int i = 0; i < size; i++) {
159             sum += arrayListPrimitiveInt.get(i).value();
160         }
161         return sum;
162     }
163 
164     @Benchmark
165     public int sumListOfPrimitiveInt() {
166         int sum = 0;
167         for (int i = 0; i < size; i++) {
168             sum += arrayListOfPrimitiveInt.get(i).value();
169         }
170         return sum;
171     }
172 
173     @Benchmark
174     public int thrashListInt() {
175         final ArrayListInt list = arrayListInt;
176 
177         int sum = 0;
178         for (int i = 0; i < 1000; i++) {
179             int ndx = (random.nextInt() & 0x7fffffff) % list.size();    // positive
180             if (list.size() == size) {
181                 list.remove(ndx);
182             } else {
183                 list.add(ndx);
184             }
185             sum += ndx;
186         }
187         return sum;
188     }
189 
190     @Benchmark
191     public int thrashListPrimitiveInt() {
192         final ArrayListPrimitiveInt list = arrayListPrimitiveInt;
193         int sum = 0;
194         for (int i = 0; i < 1000; i++) {
195             int ndx = (random.nextInt() & 0x7fffffff) % list.size();    // positive
196             if (list.size() == size) {
197                 list.remove(ndx);
198             } else {
199                 list.add(ndx, new PrimitiveInt(ndx));
200             }
201             sum += ndx;
202         }
203         return sum;
204     }
205 
206     @Benchmark
207     public int thrashListOfInteger() {
208         final ArrayList<Integer> list = arrayListOfInteger;
209         int sum = 0;
210         for (int i = 0; i < 1000; i++) {
211             int ndx = (random.nextInt() & 0x7fffffff) % list.size();    // positive
212             if (list.size() == size) {
213                 list.remove(ndx);
214             } else {
215                 list.add(ndx);
216             }
217             sum += ndx;
218         }
219         return sum;
220     }
221 
222 
223     @Benchmark
224     public int thrashListOfPrimitiveInt() {
225         final ArrayList<PrimitiveInt> list = arrayListOfPrimitiveInt;
226         int sum = 0;
227 
228         for (int i = 0; i < 1000; i++) {
229             int ndx = (random.nextInt() & 0x7fffffff) % list.size();    // positive
230             if (list.size() == size) {
231                 list.remove(ndx);
232             } else {
233                 list.add(ndx, new PrimitiveInt(ndx));
234             }
235             sum += ndx;
236         }
237         return sum;
238     }
239 }