Skip to content

Commit 89e8d66

Browse files
veontomopivovarit
authored andcommitted
Java Primitives versus Objects (eugenp#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
1 parent d2e6a74 commit 89e8d66

33 files changed

Lines changed: 841 additions & 0 deletions
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.primitive;
2+
3+
public class BenchmarkRunner {
4+
5+
public static void main(String[] args) throws Exception {
6+
new IntPrimitiveLookup().run();
7+
new IntegerWrapperLookup().run();
8+
new FloatPrimitiveLookup().run();
9+
new FloatWrapperLookup().run();
10+
new DoublePrimitiveLookup().run();
11+
new DoubleWrapperLookup().run();
12+
new ShortPrimitiveLookup().run();
13+
new ShortWrapperLookup().run();
14+
new BooleanPrimitiveLookup().run();
15+
new BooleanWrapperLookup().run();
16+
new CharPrimitiveLookup().run();
17+
new CharacterWrapperLookup().run();
18+
new BytePrimitiveLookup().run();
19+
new ByteWrapperLookup().run();
20+
new LongPrimitiveLookup().run();
21+
new LongWrapperLookup().run();
22+
}
23+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.primitive;
2+
3+
import org.openjdk.jmh.annotations.*;
4+
5+
@State(Scope.Thread)
6+
public class BooleanPrimitiveLookup extends Lookup {
7+
8+
private boolean[] elements;
9+
private final boolean pivot = false;
10+
11+
@Setup
12+
@Override
13+
public void prepare() {
14+
elements = new boolean[s];
15+
for (int i = 0; i < s - 1; i++) {
16+
elements[i] = true;
17+
}
18+
elements[s - 1] = pivot;
19+
}
20+
21+
@TearDown
22+
@Override
23+
public void clean() {
24+
elements = null;
25+
}
26+
27+
28+
@Benchmark
29+
@BenchmarkMode(Mode.AverageTime)
30+
public int findPosition() {
31+
int index = 0;
32+
while (pivot != elements[index]) {
33+
index++;
34+
}
35+
return index;
36+
}
37+
38+
@Override
39+
public String getSimpleClassName() {
40+
return BooleanPrimitiveLookup.class.getSimpleName();
41+
}
42+
43+
44+
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.primitive;
2+
3+
import org.openjdk.jmh.annotations.*;
4+
5+
@State(Scope.Thread)
6+
public class BooleanWrapperLookup extends Lookup {
7+
private Boolean[] elements;
8+
private final boolean pivot = false;
9+
10+
@Override
11+
@Setup
12+
public void prepare() {
13+
elements = new Boolean[s];
14+
for (int i = 0; i < s - 1; i++) {
15+
elements[i] = true;
16+
}
17+
elements[s - 1] = pivot;
18+
}
19+
20+
@Override
21+
@TearDown
22+
public void clean() {
23+
elements = null;
24+
}
25+
26+
@Override
27+
@Benchmark
28+
@BenchmarkMode(Mode.AverageTime)
29+
public int findPosition() {
30+
int index = 0;
31+
Boolean pivotWrapper = pivot;
32+
while (!pivotWrapper.equals(elements[index])) {
33+
index++;
34+
}
35+
return index;
36+
37+
}
38+
39+
@Override
40+
public String getSimpleClassName() {
41+
return BooleanWrapperLookup.class.getSimpleName();
42+
}
43+
44+
45+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.primitive;
2+
3+
import org.openjdk.jmh.annotations.*;
4+
5+
@State(Scope.Thread)
6+
public class BytePrimitiveLookup extends Lookup {
7+
8+
private byte[] elements;
9+
private final byte pivot = 2;
10+
11+
@Setup
12+
@Override
13+
public void prepare() {
14+
byte common = 1;
15+
elements = new byte[s];
16+
for (int i = 0; i < s - 1; i++) {
17+
elements[i] = common;
18+
}
19+
elements[s - 1] = pivot;
20+
}
21+
22+
@TearDown
23+
@Override
24+
public void clean() {
25+
elements = null;
26+
}
27+
28+
29+
@Benchmark
30+
@BenchmarkMode(Mode.AverageTime)
31+
public int findPosition() {
32+
int index = 0;
33+
while (pivot != elements[index]) {
34+
index++;
35+
}
36+
return index;
37+
}
38+
39+
@Override
40+
public String getSimpleClassName() {
41+
return BytePrimitiveLookup.class.getSimpleName();
42+
}
43+
44+
45+
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.primitive;
2+
3+
import org.openjdk.jmh.annotations.*;
4+
5+
@State(Scope.Thread)
6+
public class ByteWrapperLookup extends Lookup {
7+
private Byte[] elements;
8+
private final byte pivot = 2;
9+
10+
@Override
11+
@Setup
12+
public void prepare() {
13+
byte common = 1;
14+
elements = new Byte[s];
15+
for (int i = 0; i < s - 1; i++) {
16+
elements[i] = common;
17+
}
18+
elements[s - 1] = pivot;
19+
}
20+
21+
@Override
22+
@TearDown
23+
public void clean() {
24+
elements = null;
25+
}
26+
27+
@Override
28+
@Benchmark
29+
@BenchmarkMode(Mode.AverageTime)
30+
public int findPosition() {
31+
int index = 0;
32+
Byte pivotWrapper = pivot;
33+
while (!pivotWrapper.equals(elements[index])) {
34+
index++;
35+
}
36+
return index;
37+
38+
}
39+
40+
@Override
41+
public String getSimpleClassName() {
42+
return ByteWrapperLookup.class.getSimpleName();
43+
}
44+
45+
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.primitive;
2+
3+
import org.openjdk.jmh.annotations.*;
4+
5+
@State(Scope.Thread)
6+
public class CharPrimitiveLookup extends Lookup {
7+
8+
private char[] elements;
9+
private final char pivot = 'b';
10+
11+
@Setup
12+
@Override
13+
public void prepare() {
14+
char common = 'a';
15+
elements = new char[s];
16+
for (int i = 0; i < s - 1; i++) {
17+
elements[i] = common;
18+
}
19+
elements[s - 1] = pivot;
20+
}
21+
22+
@TearDown
23+
@Override
24+
public void clean() {
25+
elements = null;
26+
}
27+
28+
29+
@Benchmark
30+
@BenchmarkMode(Mode.AverageTime)
31+
public int findPosition() {
32+
int index = 0;
33+
while (pivot != elements[index]) {
34+
index++;
35+
}
36+
return index;
37+
}
38+
39+
@Override
40+
public String getSimpleClassName() {
41+
return CharPrimitiveLookup.class.getSimpleName();
42+
}
43+
44+
45+
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.primitive;
2+
3+
import org.openjdk.jmh.annotations.*;
4+
5+
@State(Scope.Thread)
6+
public class CharacterWrapperLookup extends Lookup {
7+
private Character[] elements;
8+
private final char pivot = 'b';
9+
10+
@Override
11+
@Setup
12+
public void prepare() {
13+
char common = 'a';
14+
elements = new Character[s];
15+
for (int i = 0; i < s - 1; i++) {
16+
elements[i] = common;
17+
}
18+
elements[s - 1] = pivot;
19+
}
20+
21+
@Override
22+
@TearDown
23+
public void clean() {
24+
elements = null;
25+
}
26+
27+
@Override
28+
@Benchmark
29+
@BenchmarkMode(Mode.AverageTime)
30+
public int findPosition() {
31+
int index = 0;
32+
Character pivotWrapper = pivot;
33+
while (!pivotWrapper.equals(elements[index])) {
34+
index++;
35+
}
36+
return index;
37+
38+
}
39+
40+
@Override
41+
public String getSimpleClassName() {
42+
return CharacterWrapperLookup.class.getSimpleName();
43+
}
44+
45+
46+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.primitive;
2+
3+
import org.openjdk.jmh.annotations.*;
4+
5+
@State(Scope.Thread)
6+
public class DoublePrimitiveLookup extends Lookup {
7+
private double[] elements;
8+
private final double pivot = 2;
9+
10+
@Setup
11+
@Override
12+
public void prepare() {
13+
double common = 1;
14+
elements = new double[s];
15+
for (int i = 0; i < s - 1; i++) {
16+
elements[i] = common;
17+
}
18+
elements[s - 1] = pivot;
19+
}
20+
21+
@TearDown
22+
@Override
23+
public void clean() {
24+
elements = null;
25+
}
26+
27+
28+
@Benchmark
29+
@BenchmarkMode(Mode.AverageTime)
30+
public int findPosition() {
31+
int index = 0;
32+
while (pivot != elements[index]) {
33+
index++;
34+
}
35+
return index;
36+
}
37+
38+
@Override
39+
public String getSimpleClassName() {
40+
return DoublePrimitiveLookup.class.getSimpleName();
41+
}
42+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.primitive;
2+
3+
import org.openjdk.jmh.annotations.*;
4+
5+
@State(Scope.Thread)
6+
public class DoubleWrapperLookup extends Lookup {
7+
private Double[] elements;
8+
private final double pivot = 2d;
9+
10+
@Override
11+
@Setup
12+
public void prepare() {
13+
double common = 1;
14+
elements = new Double[s];
15+
for (int i = 0; i < s - 1; i++) {
16+
elements[i] = common;
17+
}
18+
elements[s - 1] = pivot;
19+
}
20+
21+
@Override
22+
@TearDown
23+
public void clean() {
24+
elements = null;
25+
}
26+
27+
@Override
28+
@Benchmark
29+
@BenchmarkMode(Mode.AverageTime)
30+
public int findPosition() {
31+
int index = 0;
32+
Double pivotWrapper = pivot;
33+
while (!pivotWrapper.equals(elements[index])) {
34+
index++;
35+
}
36+
return index;
37+
38+
}
39+
40+
@Override
41+
public String getSimpleClassName() {
42+
return DoubleWrapperLookup.class.getSimpleName();
43+
}
44+
45+
}

0 commit comments

Comments
 (0)