Java Primitives versus Objects (#4946)
* Add code for the article 'Java Primitives versus Objects' * Use JMH for benchmarking the primitive and wrapper classes * Uncomment the benchmarks and remove unused class * Add Long/long to the benchmark * Remove assertions from the benchmarks * Add the java docs
This commit is contained in:
parent
d2e6a740df
commit
89e8d66def
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
56
core-java-8/src/main/java/com/baeldung/primitive/Lookup.java
Normal file
56
core-java-8/src/main/java/com/baeldung/primitive/Lookup.java
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
13
out/production/main/com/baeldung/.gitignore
vendored
Normal file
13
out/production/main/com/baeldung/.gitignore
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
*.class
|
||||||
|
|
||||||
|
#folders#
|
||||||
|
/target
|
||||||
|
/neoDb*
|
||||||
|
/data
|
||||||
|
/src/main/webapp/WEB-INF/classes
|
||||||
|
*/META-INF/*
|
||||||
|
|
||||||
|
# Packaged files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.ear
|
2
out/production/main/com/baeldung/README.md
Normal file
2
out/production/main/com/baeldung/README.md
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
### Relevant Articles:
|
||||||
|
- [SHA-256 Hashing in Java](http://www.baeldung.com/sha-256-hashing-java)
|
2
out/production/main/com/baeldung/enums/README.md
Normal file
2
out/production/main/com/baeldung/enums/README.md
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
### Relevant Articles:
|
||||||
|
- [A Guide to Java Enums](http://www.baeldung.com/a-guide-to-java-enums)
|
5
out/production/main/com/baeldung/networking/README.md
Normal file
5
out/production/main/com/baeldung/networking/README.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
### Relevant Articles:
|
||||||
|
- [A Guide To UDP In Java](http://www.baeldung.com/udp-in-java)
|
||||||
|
- [A Guide To HTTP Cookies In Java](http://www.baeldung.com/cookies-java)
|
||||||
|
- [A Guide to the Java URL](http://www.baeldung.com/java-url)
|
||||||
|
- [Working with Network Interfaces in Java](http://www.baeldung.com/java-network-interfaces)
|
1
out/production/main/com/baeldung/objectsize/MANIFEST.MF
Normal file
1
out/production/main/com/baeldung/objectsize/MANIFEST.MF
Normal file
@ -0,0 +1 @@
|
|||||||
|
Premain-class: com.baeldung.objectsize.InstrumentationAgent
|
2
out/production/main/com/baeldung/printscreen/README.md
Normal file
2
out/production/main/com/baeldung/printscreen/README.md
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
### Relevant Articles:
|
||||||
|
- [How to Print Screen in Java](http://www.baeldung.com/print-screen-in-java)
|
2
out/production/main/javac-args/arguments
Normal file
2
out/production/main/javac-args/arguments
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
-d javac-target -verbose
|
||||||
|
com/baeldung/javac/Data.java
|
2
out/production/main/javac-args/options
Normal file
2
out/production/main/javac-args/options
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
-d javac-target
|
||||||
|
-verbose
|
1
out/production/main/javac-args/types
Normal file
1
out/production/main/javac-args/types
Normal file
@ -0,0 +1 @@
|
|||||||
|
com/baeldung/javac/Data.java
|
3
out/production/main/javac-args/xlint-ops
Normal file
3
out/production/main/javac-args/xlint-ops
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
-d javac-target
|
||||||
|
-Xlint:rawtypes,unchecked,static,cast,serial,fallthrough
|
||||||
|
com/baeldung/javac/Data.java
|
9
out/production/main/log4j.properties
Normal file
9
out/production/main/log4j.properties
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# Set root logger level to DEBUG and its only appender to A1.
|
||||||
|
log4j.rootLogger=DEBUG, A1
|
||||||
|
|
||||||
|
# A1 is set to be a ConsoleAppender.
|
||||||
|
log4j.appender.A1=org.apache.log4j.ConsoleAppender
|
||||||
|
|
||||||
|
# A1 uses PatternLayout.
|
||||||
|
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
|
||||||
|
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
|
2
out/production/main1/com/baeldung/datetime/README.md
Normal file
2
out/production/main1/com/baeldung/datetime/README.md
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
### Relevant Articles:
|
||||||
|
- [Introduction to the Java 8 Date/Time API](http://www.baeldung.com/java-8-date-time-intro)
|
2
out/test/test/com/baeldung/hexToAscii/README.md
Normal file
2
out/test/test/com/baeldung/hexToAscii/README.md
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
### Relevant Articles:
|
||||||
|
- [Convert Hex to ASCII in Java](http://www.baeldung.com/java-convert-hex-to-ascii)
|
2
out/test/test/com/baeldung/java/conversion/README.md
Normal file
2
out/test/test/com/baeldung/java/conversion/README.md
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
Relevant Articles:
|
||||||
|
- [Java String Conversions](http://www.baeldung.com/java-string-conversions)
|
BIN
out/test/test/com/baeldung/stringisnumeric.zip
Normal file
BIN
out/test/test/com/baeldung/stringisnumeric.zip
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user