|
| 1 | +package com.crossoverjie.basic; |
| 2 | + |
| 3 | +import org.openjdk.jmh.annotations.*; |
| 4 | +import org.openjdk.jmh.runner.Runner; |
| 5 | +import org.openjdk.jmh.runner.RunnerException; |
| 6 | +import org.openjdk.jmh.runner.options.Options; |
| 7 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.LinkedList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.concurrent.TimeUnit; |
| 13 | + |
| 14 | +/** |
| 15 | + * Function: |
| 16 | + * |
| 17 | + * @author crossoverJie |
| 18 | + * Date: 2019-06-27 00:11 |
| 19 | + * @since JDK 1.8 |
| 20 | + */ |
| 21 | +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 22 | +@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 23 | +public class CollectionsTest { |
| 24 | + |
| 25 | + private static final int TEN_MILLION = 10000000; |
| 26 | + |
| 27 | + @Benchmark |
| 28 | + @BenchmarkMode(Mode.AverageTime) |
| 29 | + @OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 30 | + public void arrayList() { |
| 31 | + |
| 32 | + List<String> array = new ArrayList<>(); |
| 33 | + |
| 34 | + for (int i = 0; i < TEN_MILLION; i++) { |
| 35 | + array.add("123"); |
| 36 | + } |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + @Benchmark |
| 41 | + @BenchmarkMode(Mode.AverageTime) |
| 42 | + @OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 43 | + public void arrayListSize() { |
| 44 | + List<String> array = new ArrayList<>(TEN_MILLION); |
| 45 | + |
| 46 | + for (int i = 0; i < TEN_MILLION; i++) { |
| 47 | + array.add("123"); |
| 48 | + } |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + @Benchmark |
| 53 | + @BenchmarkMode(Mode.AverageTime) |
| 54 | + @OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 55 | + public void linkedList() { |
| 56 | + List<String> array = new LinkedList<>(); |
| 57 | + |
| 58 | + for (int i = 0; i < TEN_MILLION; i++) { |
| 59 | + array.add("123"); |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + public static void main(String[] args) throws RunnerException { |
| 66 | + Options opt = new OptionsBuilder() |
| 67 | + .include(CollectionsTest.class.getSimpleName()) |
| 68 | + .forks(1) |
| 69 | + .build(); |
| 70 | + |
| 71 | + |
| 72 | + new Runner(opt).run(); |
| 73 | + } |
| 74 | +} |
0 commit comments