Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/util/StringJoinerBenchmark.java
41161 views
1
/*
2
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
package org.openjdk.bench.java.util;
24
25
import org.openjdk.jmh.annotations.Benchmark;
26
import org.openjdk.jmh.annotations.BenchmarkMode;
27
import org.openjdk.jmh.annotations.Fork;
28
import org.openjdk.jmh.annotations.Measurement;
29
import org.openjdk.jmh.annotations.Mode;
30
import org.openjdk.jmh.annotations.OutputTimeUnit;
31
import org.openjdk.jmh.annotations.Param;
32
import org.openjdk.jmh.annotations.Scope;
33
import org.openjdk.jmh.annotations.Setup;
34
import org.openjdk.jmh.annotations.State;
35
import org.openjdk.jmh.annotations.Warmup;
36
37
import java.util.StringJoiner;
38
import java.util.concurrent.ThreadLocalRandom;
39
import java.util.concurrent.TimeUnit;
40
41
/**
42
* Trivial benchmark for String joining with {@link java.util.StringJoiner}.
43
*/
44
@BenchmarkMode(Mode.AverageTime)
45
@OutputTimeUnit(TimeUnit.NANOSECONDS)
46
@Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
47
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
48
@Fork(value = 3, jvmArgsAppend = {"-Xms1g", "-Xmx1g"})
49
public class StringJoinerBenchmark {
50
51
@Benchmark
52
public String join(Data data) {
53
String[] stringArray = data.stringArray;
54
return String.join(",", stringArray);
55
}
56
57
@Benchmark
58
public String stringJoiner(Data data) {
59
String[] stringArray = data.stringArray;
60
return Joiner.joinWithStringJoiner(stringArray);
61
}
62
63
@State(Scope.Thread)
64
public static class Data {
65
66
@Param({"latin", "cyrillic"})
67
private String mode;
68
69
@Param({"1", "8", "32", "128"})
70
private int length;
71
72
@Param({"5", "20"})
73
private int count;
74
75
private String[] stringArray;
76
77
@Setup
78
public void setup() {
79
stringArray = new String[count];
80
81
for (int i = 0; i < count; i++) {
82
String alphabet = getAlphabet(i, mode);
83
stringArray[i] = randomString(alphabet, length);
84
}
85
}
86
87
private String randomString(String alphabet, int length) {
88
var tl = ThreadLocalRandom.current();
89
StringBuilder sb = new StringBuilder();
90
for (int i = 0; i < length; i++) {
91
sb.append(alphabet.charAt(tl.nextInt(alphabet.length())));
92
}
93
return sb.toString();
94
}
95
96
private static String getAlphabet(int index, String mode) {
97
var latin = "abcdefghijklmnopqrstuvwxyz"; //English
98
StringBuilder sb = new StringBuilder();
99
latin.codePoints().forEach(cp -> sb.append(cp - 'a' + '\u0430'));
100
var cyrillic = sb.toString(); // Russian (partial, matching length of latin alphabet)
101
102
String alphabet;
103
switch (mode) {
104
case "latin" -> alphabet = latin;
105
case "cyrillic" -> alphabet = cyrillic;
106
default -> throw new RuntimeException("Illegal mode " + mode);
107
}
108
return alphabet;
109
}
110
}
111
}
112
113
class Joiner {
114
public static String joinWithStringJoiner(String[] stringArray) {
115
StringJoiner joiner = new StringJoiner(",", "[", "]");
116
for (String str : stringArray) {
117
joiner.add(str);
118
}
119
return joiner.toString();
120
}
121
}
122
123