[BAEL-13506] - Split or move core-java-8 module (#7790)

This commit is contained in:
Amit Pandey
2019-09-20 19:00:34 +05:30
committed by Josh Cummings
parent 7d65b1fb24
commit f2cac1ab7c
124 changed files with 640 additions and 51 deletions

View File

@@ -3,3 +3,4 @@
## Core Java Lang Cookbooks and Examples
### Relevant Articles:
- [Java Primitives versus Objects](https://www.baeldung.com/java-primitives-vs-objects)

View File

@@ -15,6 +15,21 @@
</parent>
<dependencies>
<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.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-bytecode</artifactId>
<version>${jmh-generator.version}</version>
</dependency>
</dependencies>
<build>
@@ -28,6 +43,8 @@
</build>
<properties>
<jmh-core.version>1.19</jmh-core.version>
<jmh-generator.version>1.19</jmh-generator.version>
</properties>
</project>

View File

@@ -0,0 +1,23 @@
package com.baeldung.primitive;
public class BenchmarkRunner {
public static void main(String[] args) throws Exception {
new IntPrimitiveLookup().run();
new IntegerWrapperLookup().run();
new FloatPrimitiveLookup().run();
new FloatWrapperLookup().run();
new DoublePrimitiveLookup().run();
new DoubleWrapperLookup().run();
new ShortPrimitiveLookup().run();
new ShortWrapperLookup().run();
new BooleanPrimitiveLookup().run();
new BooleanWrapperLookup().run();
new CharPrimitiveLookup().run();
new CharacterWrapperLookup().run();
new BytePrimitiveLookup().run();
new ByteWrapperLookup().run();
new LongPrimitiveLookup().run();
new LongWrapperLookup().run();
}
}

View File

@@ -0,0 +1,45 @@
package com.baeldung.primitive;
import org.openjdk.jmh.annotations.*;
@State(Scope.Thread)
public class BooleanPrimitiveLookup extends Lookup {
private boolean[] elements;
private final boolean pivot = false;
@Setup
@Override
public void prepare() {
elements = new boolean[s];
for (int i = 0; i < s - 1; i++) {
elements[i] = true;
}
elements[s - 1] = pivot;
}
@TearDown
@Override
public void clean() {
elements = null;
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int findPosition() {
int index = 0;
while (pivot != elements[index]) {
index++;
}
return index;
}
@Override
public String getSimpleClassName() {
return BooleanPrimitiveLookup.class.getSimpleName();
}
}

View File

@@ -0,0 +1,45 @@
package com.baeldung.primitive;
import org.openjdk.jmh.annotations.*;
@State(Scope.Thread)
public class BooleanWrapperLookup extends Lookup {
private Boolean[] elements;
private final boolean pivot = false;
@Override
@Setup
public void prepare() {
elements = new Boolean[s];
for (int i = 0; i < s - 1; i++) {
elements[i] = true;
}
elements[s - 1] = pivot;
}
@Override
@TearDown
public void clean() {
elements = null;
}
@Override
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int findPosition() {
int index = 0;
Boolean pivotWrapper = pivot;
while (!pivotWrapper.equals(elements[index])) {
index++;
}
return index;
}
@Override
public String getSimpleClassName() {
return BooleanWrapperLookup.class.getSimpleName();
}
}

View File

@@ -0,0 +1,46 @@
package com.baeldung.primitive;
import org.openjdk.jmh.annotations.*;
@State(Scope.Thread)
public class BytePrimitiveLookup extends Lookup {
private byte[] elements;
private final byte pivot = 2;
@Setup
@Override
public void prepare() {
byte common = 1;
elements = new byte[s];
for (int i = 0; i < s - 1; i++) {
elements[i] = common;
}
elements[s - 1] = pivot;
}
@TearDown
@Override
public void clean() {
elements = null;
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int findPosition() {
int index = 0;
while (pivot != elements[index]) {
index++;
}
return index;
}
@Override
public String getSimpleClassName() {
return BytePrimitiveLookup.class.getSimpleName();
}
}

View File

@@ -0,0 +1,46 @@
package com.baeldung.primitive;
import org.openjdk.jmh.annotations.*;
@State(Scope.Thread)
public class ByteWrapperLookup extends Lookup {
private Byte[] elements;
private final byte pivot = 2;
@Override
@Setup
public void prepare() {
byte common = 1;
elements = new Byte[s];
for (int i = 0; i < s - 1; i++) {
elements[i] = common;
}
elements[s - 1] = pivot;
}
@Override
@TearDown
public void clean() {
elements = null;
}
@Override
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int findPosition() {
int index = 0;
Byte pivotWrapper = pivot;
while (!pivotWrapper.equals(elements[index])) {
index++;
}
return index;
}
@Override
public String getSimpleClassName() {
return ByteWrapperLookup.class.getSimpleName();
}
}

View File

@@ -0,0 +1,46 @@
package com.baeldung.primitive;
import org.openjdk.jmh.annotations.*;
@State(Scope.Thread)
public class CharPrimitiveLookup extends Lookup {
private char[] elements;
private final char pivot = 'b';
@Setup
@Override
public void prepare() {
char common = 'a';
elements = new char[s];
for (int i = 0; i < s - 1; i++) {
elements[i] = common;
}
elements[s - 1] = pivot;
}
@TearDown
@Override
public void clean() {
elements = null;
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int findPosition() {
int index = 0;
while (pivot != elements[index]) {
index++;
}
return index;
}
@Override
public String getSimpleClassName() {
return CharPrimitiveLookup.class.getSimpleName();
}
}

View File

@@ -0,0 +1,46 @@
package com.baeldung.primitive;
import org.openjdk.jmh.annotations.*;
@State(Scope.Thread)
public class CharacterWrapperLookup extends Lookup {
private Character[] elements;
private final char pivot = 'b';
@Override
@Setup
public void prepare() {
char common = 'a';
elements = new Character[s];
for (int i = 0; i < s - 1; i++) {
elements[i] = common;
}
elements[s - 1] = pivot;
}
@Override
@TearDown
public void clean() {
elements = null;
}
@Override
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int findPosition() {
int index = 0;
Character pivotWrapper = pivot;
while (!pivotWrapper.equals(elements[index])) {
index++;
}
return index;
}
@Override
public String getSimpleClassName() {
return CharacterWrapperLookup.class.getSimpleName();
}
}

View File

@@ -0,0 +1,42 @@
package com.baeldung.primitive;
import org.openjdk.jmh.annotations.*;
@State(Scope.Thread)
public class DoublePrimitiveLookup extends Lookup {
private double[] elements;
private final double pivot = 2;
@Setup
@Override
public void prepare() {
double common = 1;
elements = new double[s];
for (int i = 0; i < s - 1; i++) {
elements[i] = common;
}
elements[s - 1] = pivot;
}
@TearDown
@Override
public void clean() {
elements = null;
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int findPosition() {
int index = 0;
while (pivot != elements[index]) {
index++;
}
return index;
}
@Override
public String getSimpleClassName() {
return DoublePrimitiveLookup.class.getSimpleName();
}
}

View File

@@ -0,0 +1,45 @@
package com.baeldung.primitive;
import org.openjdk.jmh.annotations.*;
@State(Scope.Thread)
public class DoubleWrapperLookup extends Lookup {
private Double[] elements;
private final double pivot = 2d;
@Override
@Setup
public void prepare() {
double common = 1;
elements = new Double[s];
for (int i = 0; i < s - 1; i++) {
elements[i] = common;
}
elements[s - 1] = pivot;
}
@Override
@TearDown
public void clean() {
elements = null;
}
@Override
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int findPosition() {
int index = 0;
Double pivotWrapper = pivot;
while (!pivotWrapper.equals(elements[index])) {
index++;
}
return index;
}
@Override
public String getSimpleClassName() {
return DoubleWrapperLookup.class.getSimpleName();
}
}

View File

@@ -0,0 +1,42 @@
package com.baeldung.primitive;
import org.openjdk.jmh.annotations.*;
@State(Scope.Thread)
public class FloatPrimitiveLookup extends Lookup {
private float[] elements;
private final float pivot = 2;
@Setup
@Override
public void prepare() {
int common = 1;
elements = new float[s];
for (int i = 0; i < s - 1; i++) {
elements[i] = common;
}
elements[s - 1] = pivot;
}
@TearDown
@Override
public void clean() {
elements = null;
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int findPosition() {
int index = 0;
while (pivot != elements[index]) {
index++;
}
return index;
}
@Override
public String getSimpleClassName() {
return FloatPrimitiveLookup.class.getSimpleName();
}
}

View File

@@ -0,0 +1,43 @@
package com.baeldung.primitive;
import org.openjdk.jmh.annotations.*;
@State(Scope.Thread)
public class FloatWrapperLookup extends Lookup {
private Float[] elements;
private final float pivot = 2;
@Override
@Setup
public void prepare() {
float common = 1;
elements = new Float[s];
for (int i = 0; i < s - 1; i++) {
elements[i] = common;
}
elements[s - 1] = pivot;
}
@Override
@TearDown
public void clean() {
elements = null;
}
@Override
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int findPosition() {
int index = 0;
Float pivotWrapper = pivot;
while (!pivotWrapper.equals(elements[index])) {
index++;
}
return index;
}
@Override
public String getSimpleClassName() {
return FloatWrapperLookup.class.getSimpleName();
}
}

View File

@@ -0,0 +1,45 @@
package com.baeldung.primitive;
import org.openjdk.jmh.annotations.*;
@State(Scope.Thread)
public class IntPrimitiveLookup extends Lookup {
private int[] elements;
private final int pivot = 2;
@Setup
@Override
public void prepare() {
int common = 1;
elements = new int[s];
for (int i = 0; i < s - 1; i++) {
elements[i] = common;
}
elements[s - 1] = pivot;
}
@TearDown
@Override
public void clean() {
elements = null;
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int findPosition() {
int index = 0;
while (pivot != elements[index]) {
index++;
}
return index;
}
@Override
public String getSimpleClassName() {
return IntPrimitiveLookup.class.getSimpleName();
}
}

View File

@@ -0,0 +1,45 @@
package com.baeldung.primitive;
import org.openjdk.jmh.annotations.*;
@State(Scope.Thread)
public class IntegerWrapperLookup extends Lookup {
private Integer[] elements;
private final int pivot = 2;
@Override
@Setup
public void prepare() {
int common = 1;
elements = new Integer[s];
for (int i = 0; i < s - 1; i++) {
elements[i] = common;
}
elements[s - 1] = pivot;
}
@Override
@TearDown
public void clean() {
elements = null;
}
@Override
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int findPosition() {
int index = 0;
Integer pivotWrapper = pivot;
while (!pivotWrapper.equals(elements[index])) {
index++;
}
return index;
}
@Override
public String getSimpleClassName() {
return IntegerWrapperLookup.class.getSimpleName();
}
}

View File

@@ -0,0 +1,42 @@
package com.baeldung.primitive;
import org.openjdk.jmh.annotations.*;
@State(Scope.Thread)
public class LongPrimitiveLookup extends Lookup {
private long[] elements;
private final long pivot = 2;
@Setup
@Override
public void prepare() {
long common = 1;
elements = new long[s];
for (int i = 0; i < s - 1; i++) {
elements[i] = common;
}
elements[s - 1] = pivot;
}
@TearDown
@Override
public void clean() {
elements = null;
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int findPosition() {
int index = 0;
while (pivot != elements[index]) {
index++;
}
return index;
}
@Override
public String getSimpleClassName() {
return LongPrimitiveLookup.class.getSimpleName();
}
}

View File

@@ -0,0 +1,45 @@
package com.baeldung.primitive;
import org.openjdk.jmh.annotations.*;
@State(Scope.Thread)
public class LongWrapperLookup extends Lookup{
private Long[] elements;
private final long pivot = 2;
@Override
@Setup
public void prepare() {
long common = 1;
elements = new Long[s];
for (int i = 0; i < s - 1; i++) {
elements[i] = common;
}
elements[s - 1] = pivot;
}
@Override
@TearDown
public void clean() {
elements = null;
}
@Override
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int findPosition() {
int index = 0;
Long pivotWrapper = pivot;
while (!pivotWrapper.equals(elements[index])) {
index++;
}
return index;
}
@Override
public String getSimpleClassName() {
return LongWrapperLookup.class.getSimpleName();
}
}

View File

@@ -0,0 +1,56 @@
package com.baeldung.primitive;
import org.openjdk.jmh.results.RunResult;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.Collection;
/**
* An abstract class that is to be extended by the classes that
* perform lookup in the arrays either of Java primitive types or their wrappers.
*/
public abstract class Lookup {
/**
* the array size
*/
final protected int s = 50000000;
/**
* Initialize the array: fill in the array with the same
* elements except for the last one.
*/
abstract public void prepare();
/**
* Free the array's reference.
*/
abstract public void clean();
/**
* Find the position of the element that is different from the others.
* By construction, it is the last array element.
*
* @return array's last element index
*/
abstract public int findPosition();
/**
* Get the name of the class that extends this one. It is needed in order
* to set up the benchmark.
*
* @return
*/
abstract public String getSimpleClassName();
Collection<RunResult> run() throws RunnerException {
Options opt = new OptionsBuilder()
.include(getSimpleClassName())
.forks(1)
.build();
return new Runner(opt).run();
}
}

View File

@@ -0,0 +1,45 @@
package com.baeldung.primitive;
import org.openjdk.jmh.annotations.*;
@State(Scope.Thread)
public class ShortPrimitiveLookup extends Lookup {
private short[] elements;
private final short pivot = 2;
@Setup
@Override
public void prepare() {
short common = 1;
elements = new short[s];
for (int i = 0; i < s - 1; i++) {
elements[i] = common;
}
elements[s - 1] = pivot;
}
@TearDown
@Override
public void clean() {
elements = null;
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int findPosition() {
int index = 0;
while (pivot != elements[index]) {
index++;
}
return index;
}
@Override
public String getSimpleClassName() {
return ShortPrimitiveLookup.class.getSimpleName();
}
}

View File

@@ -0,0 +1,46 @@
package com.baeldung.primitive;
import org.openjdk.jmh.annotations.*;
@State(Scope.Thread)
public class ShortWrapperLookup extends Lookup {
private Short[] elements;
private final short pivot = 2;
@Override
@Setup
public void prepare() {
short common = 1;
elements = new Short[s];
for (int i = 0; i < s - 1; i++) {
elements[i] = common;
}
elements[s - 1] = pivot;
}
@Override
@TearDown
public void clean() {
elements = null;
}
@Override
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int findPosition() {
int index = 0;
Short pivotWrapper = pivot;
while (!pivotWrapper.equals(elements[index])) {
index++;
}
return index;
}
@Override
public String getSimpleClassName() {
return ShortWrapperLookup.class.getSimpleName();
}
}