Skip to content

Commit fdf1106

Browse files
committed
feat: Java 代码性能提升技巧(https://www.wdbyte.com/java/code-5-tips.html)
1 parent da41717 commit fdf1106

File tree

7 files changed

+346
-0
lines changed

7 files changed

+346
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## core-java-performance-code
2+
当前模块包含代码性能相关代码
3+
4+
### 相关文章
5+
6+
- [Java 中的 5 个代码性能提升技巧](https://www.wdbyte.com/java/code-5-tips.html)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>core-java-modules</artifactId>
7+
<groupId>com.wdbyte.core-java-modules</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<artifactId>core-java-performance-code</artifactId>
12+
<version>1.0.0-SNAPSHOT</version>
13+
<packaging>jar</packaging>
14+
15+
<properties>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<maven.compiler.target>1.8</maven.compiler.target>
18+
</properties>
19+
20+
<dependencies>
21+
<!-- https://mvnrepository.com/artifact/org.openjdk.jmh/jmh-core -->
22+
<dependency>
23+
<groupId>org.openjdk.jmh</groupId>
24+
<artifactId>jmh-core</artifactId>
25+
<version>1.33</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.openjdk.jmh</groupId>
29+
<artifactId>jmh-generator-annprocess</artifactId>
30+
<version>1.33</version>
31+
<scope>provided</scope>
32+
</dependency>
33+
</dependencies>
34+
35+
</project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.wdbyte;
2+
3+
import java.util.EnumSet;
4+
5+
import org.openjdk.jmh.annotations.Benchmark;
6+
import org.openjdk.jmh.annotations.Measurement;
7+
import org.openjdk.jmh.annotations.Scope;
8+
import org.openjdk.jmh.annotations.State;
9+
import org.openjdk.jmh.annotations.Warmup;
10+
import org.openjdk.jmh.infra.Blackhole;
11+
12+
/**
13+
* 枚举类遍历测试
14+
*
15+
* @author https://www.wdbyte.com
16+
* @date 2021/12/07
17+
*/
18+
@State(Scope.Benchmark)
19+
@Warmup(iterations = 3, time = 3)
20+
@Measurement(iterations = 5, time = 3)
21+
public class EnumIteration {
22+
23+
enum FourteenEnum {
24+
a, b, c, d, e, f, g,
25+
h, i, j, k, l, m, n,
26+
o, p, q, r, s, t,
27+
u, v, w, x, y, z;
28+
29+
static final FourteenEnum[] VALUES;
30+
static {
31+
VALUES = values();
32+
}
33+
}
34+
35+
@Benchmark
36+
public void valuesEnum(Blackhole bh) {
37+
for (FourteenEnum value : FourteenEnum.values()) {
38+
bh.consume(value.ordinal());
39+
}
40+
}
41+
42+
@Benchmark
43+
public void enumSetEnum(Blackhole bh) {
44+
for (FourteenEnum value : EnumSet.allOf(FourteenEnum.class)) {
45+
bh.consume(value.ordinal());
46+
}
47+
}
48+
49+
@Benchmark
50+
public void cacheEnums(Blackhole bh) {
51+
for (FourteenEnum value : FourteenEnum.VALUES) {
52+
bh.consume(value.ordinal());
53+
}
54+
}
55+
}
56+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.wdbyte;
2+
3+
import java.util.EnumMap;
4+
import java.util.HashMap;
5+
import java.util.SplittableRandom;
6+
import java.util.UUID;
7+
8+
import org.openjdk.jmh.annotations.Benchmark;
9+
import org.openjdk.jmh.annotations.Level;
10+
import org.openjdk.jmh.annotations.Measurement;
11+
import org.openjdk.jmh.annotations.Scope;
12+
import org.openjdk.jmh.annotations.Setup;
13+
import org.openjdk.jmh.annotations.State;
14+
import org.openjdk.jmh.annotations.Warmup;
15+
import org.openjdk.jmh.infra.Blackhole;
16+
17+
@State(Scope.Benchmark)
18+
@Warmup(iterations = 3, time = 3)
19+
@Measurement(iterations = 5, time = 3)
20+
public class EnumMapBenchmark {
21+
22+
enum AnEnum {
23+
a, b, c, d, e, f, g,
24+
h, i, j, k, l, m, n,
25+
o, p, q, r, s, t,
26+
u, v, w, x, y, z;
27+
}
28+
29+
/** 要查找的 key 的数量 */
30+
private static int size = 10000;
31+
/** 随机数种子 */
32+
private static int seed = 99;
33+
34+
@State(Scope.Benchmark)
35+
public static class EnumMapState {
36+
private EnumMap<AnEnum, String> map;
37+
private AnEnum[] values;
38+
39+
@Setup(Level.Trial)
40+
public void setup() {
41+
map = new EnumMap<>(AnEnum.class);
42+
values = new AnEnum[size];
43+
AnEnum[] enumValues = AnEnum.values();
44+
SplittableRandom random = new SplittableRandom(seed);
45+
for (int i = 0; i < size; i++) {
46+
int nextInt = random.nextInt(0, Integer.MAX_VALUE);
47+
values[i] = enumValues[nextInt % enumValues.length];
48+
}
49+
for (AnEnum value : enumValues) {
50+
map.put(value, UUID.randomUUID().toString());
51+
}
52+
}
53+
}
54+
55+
@State(Scope.Benchmark)
56+
public static class HashMapState{
57+
private HashMap<String, String> map;
58+
private String[] values;
59+
60+
@Setup(Level.Trial)
61+
public void setup() {
62+
map = new HashMap<>();
63+
values = new String[size];
64+
AnEnum[] enumValues = AnEnum.values();
65+
int pos = 0;
66+
SplittableRandom random = new SplittableRandom(seed);
67+
for (int i = 0; i < size; i++) {
68+
int nextInt = random.nextInt(0, Integer.MAX_VALUE);
69+
values[i] = enumValues[nextInt % enumValues.length].toString();
70+
}
71+
for (AnEnum value : enumValues) {
72+
map.put(value.toString(), UUID.randomUUID().toString());
73+
}
74+
}
75+
}
76+
77+
@Benchmark
78+
public void enumMap(EnumMapState state, Blackhole bh) {
79+
for (AnEnum value : state.values) {
80+
bh.consume(state.map.get(value));
81+
}
82+
}
83+
84+
@Benchmark
85+
public void hashMap(HashMapState state, Blackhole bh) {
86+
for (String value : state.values) {
87+
bh.consume(state.map.get(value));
88+
}
89+
}
90+
}
91+
92+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.wdbyte;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.UUID;
6+
7+
import org.apache.commons.lang3.tuple.MutablePair;
8+
import org.apache.commons.lang3.tuple.Pair;
9+
import org.openjdk.jmh.annotations.Benchmark;
10+
import org.openjdk.jmh.annotations.Level;
11+
import org.openjdk.jmh.annotations.Measurement;
12+
import org.openjdk.jmh.annotations.OperationsPerInvocation;
13+
import org.openjdk.jmh.annotations.Scope;
14+
import org.openjdk.jmh.annotations.Setup;
15+
import org.openjdk.jmh.annotations.State;
16+
import org.openjdk.jmh.annotations.Warmup;
17+
import org.openjdk.jmh.infra.Blackhole;
18+
19+
/**
20+
* @author https://www.wdbyte.com
21+
* @date 2021/12/06
22+
*/
23+
@State(Scope.Benchmark)
24+
@Warmup(iterations = 3, time = 3)
25+
@Measurement(iterations = 5, time = 3)
26+
public class HashMapKey {
27+
28+
private int size = 1024;
29+
private Map<String, Object> stringMap;
30+
private Map<Pair, Object> pairMap;
31+
private String[] prefixes;
32+
private String[] suffixes;
33+
34+
@Setup(Level.Trial)
35+
public void setup() {
36+
prefixes = new String[size];
37+
suffixes = new String[size];
38+
stringMap = new HashMap<>();
39+
pairMap = new HashMap<>();
40+
for (int i = 0; i < size; ++i) {
41+
prefixes[i] = UUID.randomUUID().toString();
42+
suffixes[i] = UUID.randomUUID().toString();
43+
stringMap.put(prefixes[i] + ";" + suffixes[i], i);
44+
// use new String to avoid reference equality speeding up the equals calls
45+
pairMap.put(new MutablePair(prefixes[i], suffixes[i]), i);
46+
}
47+
}
48+
49+
@Benchmark
50+
@OperationsPerInvocation(1024)
51+
public void stringKey(Blackhole bh) {
52+
for (int i = 0; i < prefixes.length; i++) {
53+
bh.consume(stringMap.get(prefixes[i] + ";" + suffixes[i]));
54+
}
55+
}
56+
57+
@Benchmark
58+
@OperationsPerInvocation(1024)
59+
public void pairMap(Blackhole bh) {
60+
for (int i = 0; i < prefixes.length; i++) {
61+
bh.consume(pairMap.get(new MutablePair(prefixes[i], suffixes[i])));
62+
}
63+
}
64+
65+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.wdbyte;
2+
3+
import java.util.HashMap;
4+
5+
import org.openjdk.jmh.annotations.Benchmark;
6+
import org.openjdk.jmh.annotations.Measurement;
7+
import org.openjdk.jmh.annotations.Param;
8+
import org.openjdk.jmh.annotations.Scope;
9+
import org.openjdk.jmh.annotations.State;
10+
import org.openjdk.jmh.annotations.Warmup;
11+
12+
/**
13+
* @author https://www.wdbyte.com
14+
* @date 2021/12/06
15+
*/
16+
@State(Scope.Benchmark)
17+
@Warmup(iterations = 3,time = 3)
18+
@Measurement(iterations = 5,time = 3)
19+
public class HashMapSize {
20+
21+
@Param({"14"})
22+
int keys;
23+
24+
@Param({"16", "32"})
25+
int size;
26+
27+
@Benchmark
28+
public HashMap<Integer, Integer> getHashMap() {
29+
HashMap<Integer, Integer> map = new HashMap<>(size);
30+
for (int i = 0; i < keys; i++) {
31+
map.put(i, i);
32+
}
33+
return map;
34+
}
35+
36+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.wdbyte;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.UUID;
7+
8+
import org.openjdk.jmh.annotations.Benchmark;
9+
import org.openjdk.jmh.annotations.Level;
10+
import org.openjdk.jmh.annotations.Measurement;
11+
import org.openjdk.jmh.annotations.Param;
12+
import org.openjdk.jmh.annotations.Scope;
13+
import org.openjdk.jmh.annotations.Setup;
14+
import org.openjdk.jmh.annotations.State;
15+
import org.openjdk.jmh.annotations.Warmup;
16+
import org.openjdk.jmh.infra.Blackhole;
17+
18+
/**
19+
* @author niulang
20+
* @date 2021/12/23
21+
*/
22+
@State(Scope.Benchmark)
23+
@Warmup(iterations = 3, time = 3)
24+
@Measurement(iterations = 5, time = 3)
25+
public class StringInJdk {
26+
27+
@Param({"10000"})
28+
private int size;
29+
private String[] stringArray;
30+
private List<byte[]> byteList;
31+
32+
@Setup(Level.Trial)
33+
public void setup() {
34+
byteList = new ArrayList<>(size);
35+
stringArray = new String[size];
36+
for (int i = 0; i < size; i++) {
37+
String uuid = UUID.randomUUID().toString();
38+
stringArray[i] = uuid;
39+
byteList.add(uuid.getBytes(StandardCharsets.UTF_8));
40+
}
41+
}
42+
43+
@Benchmark
44+
public void byteToString(Blackhole bh) {
45+
for (byte[] bytes : byteList) {
46+
bh.consume(new String(bytes, StandardCharsets.UTF_8));
47+
}
48+
}
49+
50+
@Benchmark
51+
public void stringToByte(Blackhole bh) {
52+
for (String s : stringArray) {
53+
bh.consume(s.getBytes(StandardCharsets.UTF_8));
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)