move the code into core-java-collections-lists module
This commit is contained in:
@@ -36,6 +36,33 @@
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator-annprocess.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.sf.trove4j</groupId>
|
||||
<artifactId>trove4j</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>it.unimi.dsi</groupId>
|
||||
<artifactId>fastutil</artifactId>
|
||||
<version>8.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>colt</groupId>
|
||||
<artifactId>colt</artifactId>
|
||||
<version>1.2.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
@@ -44,5 +71,7 @@
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
<assertj.version>3.11.1</assertj.version>
|
||||
<lombok.version>1.16.12</lombok.version>
|
||||
<jmh-core.version>1.19</jmh-core.version>
|
||||
<jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.list.primitive;
|
||||
|
||||
import com.google.common.primitives.ImmutableIntArray;
|
||||
import com.google.common.primitives.Ints;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class PrimitiveCollections {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[] primitives = new int[] {5, 10, 0, 2};
|
||||
|
||||
guavaPrimitives(primitives);
|
||||
}
|
||||
|
||||
|
||||
private static void guavaPrimitives(int[] primitives) {
|
||||
|
||||
ImmutableIntArray immutableIntArray = ImmutableIntArray.builder().addAll(primitives).build();
|
||||
System.out.println(immutableIntArray);
|
||||
|
||||
List<Integer> list = Ints.asList(primitives);
|
||||
|
||||
int[] primitiveArray = Ints.toArray(list);
|
||||
|
||||
int[] concatenated = Ints.concat(primitiveArray, primitives);
|
||||
|
||||
System.out.println(Arrays.toString(concatenated));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.baeldung.list.primitive;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import gnu.trove.list.array.TIntArrayList;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
@Warmup(iterations = 10)
|
||||
public class PrimitivesListPerformance {
|
||||
|
||||
@State(Scope.Thread)
|
||||
public static class Initialize {
|
||||
|
||||
List<Integer> arrayList = new ArrayList<>();
|
||||
TIntArrayList tList = new TIntArrayList();
|
||||
cern.colt.list.IntArrayList coltList = new cern.colt.list.IntArrayList();
|
||||
IntArrayList fastUtilList = new IntArrayList();
|
||||
|
||||
int getValue = 10;
|
||||
|
||||
final int iterations = 100000;
|
||||
|
||||
@Setup(Level.Trial)
|
||||
public void setUp() {
|
||||
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
arrayList.add(i);
|
||||
tList.add(i);
|
||||
coltList.add(i);
|
||||
fastUtilList.add(i);
|
||||
}
|
||||
|
||||
arrayList.add(getValue);
|
||||
tList.add(getValue);
|
||||
coltList.add(getValue);
|
||||
fastUtilList.add(getValue);
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean containsArrayList(PrimitivesListPerformance.Initialize state) {
|
||||
return state.arrayList.contains(state.getValue);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean containsTIntList(PrimitivesListPerformance.Initialize state) {
|
||||
return state.tList.contains(state.getValue);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean containsColtIntList(PrimitivesListPerformance.Initialize state) {
|
||||
return state.coltList.contains(state.getValue);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean containsFastUtilIntList(PrimitivesListPerformance.Initialize state) {
|
||||
return state.fastUtilList.contains(state.getValue);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Options options = new OptionsBuilder()
|
||||
.include(PrimitivesListPerformance.class.getSimpleName()).threads(1)
|
||||
.forks(1).shouldFailOnError(true)
|
||||
.shouldDoGC(true)
|
||||
.jvmArgs("-server").build();
|
||||
new Runner(options).run();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user