Add files via upload (#14006)

This commit is contained in:
Bahaa El-Din Helmy 2023-05-15 18:14:21 +03:00 committed by GitHub
parent 65f6f416c5
commit 2da5d14e25
2 changed files with 105 additions and 33 deletions

View File

@ -1,34 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-list-5</artifactId>
<name>core-java-collections-list-5</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
<properties>
<commons-lang.version>2.2</commons-lang.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
</properties>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-list-5</artifactId>
<name>core-java-collections-list-5</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</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>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</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>
</project>

View File

@ -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();
}
}