|
| 1 | +package com.baeldung.list.primitive; |
| 2 | + |
| 3 | +import it.unimi.dsi.fastutil.ints.IntArrayList; |
| 4 | +import gnu.trove.list.array.TIntArrayList; |
| 5 | +import org.openjdk.jmh.annotations.*; |
| 6 | +import org.openjdk.jmh.runner.Runner; |
| 7 | +import org.openjdk.jmh.runner.options.Options; |
| 8 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.concurrent.TimeUnit; |
| 13 | + |
| 14 | +@BenchmarkMode(Mode.SingleShotTime) |
| 15 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 16 | +@Measurement(batchSize = 100000, iterations = 10) |
| 17 | +@Warmup(batchSize = 100000, iterations = 10) |
| 18 | +@State(Scope.Thread) |
| 19 | +public class PrimitivesListPerformance { |
| 20 | + |
| 21 | + private List<Integer> arrayList = new ArrayList<>(); |
| 22 | + private TIntArrayList tList = new TIntArrayList(); |
| 23 | + private cern.colt.list.IntArrayList coltList = new cern.colt.list.IntArrayList(); |
| 24 | + private IntArrayList fastUtilList = new IntArrayList(); |
| 25 | + |
| 26 | + private int getValue = 10; |
| 27 | + |
| 28 | + @Benchmark |
| 29 | + public boolean addArrayList() { |
| 30 | + return arrayList.add(getValue); |
| 31 | + } |
| 32 | + |
| 33 | + @Benchmark |
| 34 | + public boolean addTroveIntList() { |
| 35 | + return tList.add(getValue); |
| 36 | + } |
| 37 | + |
| 38 | + @Benchmark |
| 39 | + public void addColtIntList() { |
| 40 | + coltList.add(getValue); |
| 41 | + } |
| 42 | + |
| 43 | + @Benchmark |
| 44 | + public boolean addFastUtilIntList() { |
| 45 | + return fastUtilList.add(getValue); |
| 46 | + } |
| 47 | + |
| 48 | + @Benchmark |
| 49 | + public int getArrayList() { |
| 50 | + return arrayList.get(getValue); |
| 51 | + } |
| 52 | + |
| 53 | + @Benchmark |
| 54 | + public int getTroveIntList() { |
| 55 | + return tList.get(getValue); |
| 56 | + } |
| 57 | + |
| 58 | + @Benchmark |
| 59 | + public int getColtIntList() { |
| 60 | + return coltList.get(getValue); |
| 61 | + } |
| 62 | + |
| 63 | + @Benchmark |
| 64 | + public int getFastUtilIntList() { |
| 65 | + return fastUtilList.getInt(getValue); |
| 66 | + } |
| 67 | + |
| 68 | + @Benchmark |
| 69 | + public boolean containsArrayList() { |
| 70 | + return arrayList.contains(getValue); |
| 71 | + } |
| 72 | + |
| 73 | + @Benchmark |
| 74 | + public boolean containsTroveIntList() { |
| 75 | + return tList.contains(getValue); |
| 76 | + } |
| 77 | + |
| 78 | + @Benchmark |
| 79 | + public boolean containsColtIntList() { |
| 80 | + return coltList.contains(getValue); |
| 81 | + } |
| 82 | + |
| 83 | + @Benchmark |
| 84 | + public boolean containsFastUtilIntList() { |
| 85 | + return fastUtilList.contains(getValue); |
| 86 | + } |
| 87 | + |
| 88 | + public static void main(String[] args) throws Exception { |
| 89 | + Options options = new OptionsBuilder() |
| 90 | + .include(PrimitivesListPerformance.class.getSimpleName()).threads(1) |
| 91 | + .forks(1).shouldFailOnError(true) |
| 92 | + .shouldDoGC(true) |
| 93 | + .jvmArgs("-server").build(); |
| 94 | + new Runner(options).run(); |
| 95 | + } |
| 96 | +} |
0 commit comments