Add files via upload (#14006)
This commit is contained in:
parent
65f6f416c5
commit
2da5d14e25
|
@ -14,6 +14,12 @@
|
|||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>1.36</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
|
@ -27,6 +33,7 @@
|
|||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<jmh.version>1.21</jmh.version>
|
||||
<commons-lang.version>2.2</commons-lang.version>
|
||||
<commons-lang3.version>3.12.0</commons-lang3.version>
|
||||
</properties>
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
package com.baeldung.arrayandlistperformance;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@State(Scope.Benchmark)
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public class ArrayAndArrayListPerformance {
|
||||
|
||||
@Benchmark
|
||||
public void arrayCreation() {
|
||||
int[] array = new int[1000000];
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void arrayListCreation() {
|
||||
ArrayList<Integer> list = new ArrayList<>(1000000);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void arrayItemSetting() {
|
||||
int[] array = new int[1000000];
|
||||
array[0] = 10;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void arrayListItemSetting() {
|
||||
ArrayList<Integer> list = new ArrayList<>(1000000);
|
||||
list.add(0, 10);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void arrayItemRetrieval() {
|
||||
int[] array = new int[1000000];
|
||||
array[0] = 10;
|
||||
int item = array[0];
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void arrayListItemRetrieval() {
|
||||
ArrayList<Integer> list = new ArrayList<>(1000000);
|
||||
list.add(0, 10);
|
||||
int item2 = list.get(0);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void arrayCloning() {
|
||||
int[] array = new int[1000000];
|
||||
int[] newArray = array.clone();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void arrayListCloning() {
|
||||
ArrayList<Integer> list = new ArrayList<>(1000000);
|
||||
ArrayList<Integer> newList = new ArrayList<>(list);
|
||||
}
|
||||
public static void main(String[] args) throws Exception {
|
||||
org.openjdk.jmh.runner.Runner runner = new org.openjdk.jmh.runner.Runner(new OptionsBuilder()
|
||||
.include(ArrayAndArrayListPerformance.class.getSimpleName())
|
||||
.forks(1)
|
||||
.build());
|
||||
runner.run();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue