Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/lang/StringBuilders.java
41161 views
1
/*
2
* Copyright (c) 2014, 2019, 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.lang;
24
25
import org.openjdk.jmh.annotations.Benchmark;
26
import org.openjdk.jmh.annotations.BenchmarkMode;
27
import org.openjdk.jmh.annotations.Mode;
28
import org.openjdk.jmh.annotations.OutputTimeUnit;
29
import org.openjdk.jmh.annotations.Param;
30
import org.openjdk.jmh.annotations.Scope;
31
import org.openjdk.jmh.annotations.Setup;
32
import org.openjdk.jmh.annotations.State;
33
34
import java.util.concurrent.TimeUnit;
35
36
@BenchmarkMode(Mode.AverageTime)
37
@OutputTimeUnit(TimeUnit.NANOSECONDS)
38
@State(Scope.Thread)
39
public class StringBuilders {
40
41
private String[] strings;
42
private String[] str3p4p2;
43
private String[] str16p8p7;
44
private String[] str3p9p8;
45
private String[] str22p40p31;
46
private StringBuilder sbLatin1;
47
private StringBuilder sbUtf16;
48
49
@Setup
50
public void setup() {
51
strings = new String[]{"As", "your", "attorney,", "I",
52
"advise", "you", "to", "drive", "at", "top", "speed", "it'll",
53
"be", "a", "god", "damn", "miracle", "if", "we", "can", "get",
54
"there", "before", "you", "turn", "into", "a", "wild", "animal."};
55
str3p4p2 = new String[]{"123", "1234", "12"};
56
str16p8p7 = new String[]{"1234567890123456", "12345678", "1234567"};
57
str3p9p8 = new String[]{"123", "123456789", "12345678"};
58
str22p40p31 = new String[]{"1234567890123456789012", "1234567890123456789012345678901234567890", "1234567890123456789012345678901"};
59
sbLatin1 = new StringBuilder("Latin1 string");
60
sbUtf16 = new StringBuilder("UTF-\uFF11\uFF16 string");
61
}
62
63
@Benchmark
64
public String concat3p4p2() throws Exception {
65
return new StringBuilder(String.valueOf(str3p4p2[0])).append(str3p4p2[1]).append(str3p4p2[2]).toString();
66
}
67
68
@Benchmark
69
public String concat16p8p7() throws Exception {
70
return new StringBuilder(String.valueOf(str16p8p7[0])).append(str16p8p7[1]).append(str16p8p7[2]).toString();
71
}
72
73
@Benchmark
74
public String concat3p9p8() throws Exception {
75
return new StringBuilder(String.valueOf(str3p9p8[0])).append(str3p9p8[1]).append(str3p9p8[2]).toString();
76
}
77
78
@Benchmark
79
public String concat22p40p31() throws Exception {
80
return new StringBuilder(String.valueOf(str22p40p31[0])).append(str22p40p31[1]).append(str22p40p31[2]).toString();
81
}
82
83
@Benchmark
84
public StringBuilder appendLoop8() {
85
StringBuilder sb = new StringBuilder();
86
for (int i = 0; i < 8; i++) {
87
sb.append(strings[i]);
88
}
89
return sb;
90
}
91
92
@Benchmark
93
public StringBuilder appendLoop16() {
94
StringBuilder sb = new StringBuilder();
95
for (int i = 0; i < 16; i++) {
96
sb.append(strings[i]);
97
}
98
return sb;
99
}
100
101
@Benchmark
102
public String toStringCharWithChar1() {
103
StringBuilder result = new StringBuilder();
104
result.append('a');
105
return result.toString();
106
}
107
108
@Benchmark
109
public String toStringCharWithChar2() {
110
StringBuilder result = new StringBuilder();
111
result.append('a');
112
result.append('p');
113
return result.toString();
114
}
115
116
117
@Benchmark
118
public String toStringCharWithChar4() {
119
StringBuilder result = new StringBuilder();
120
result.append('a');
121
result.append('p');
122
result.append('a');
123
result.append(' ');
124
return result.toString();
125
}
126
127
@Benchmark
128
public String toStringCharWithChar8() {
129
StringBuilder result = new StringBuilder();
130
result.append('a');
131
result.append('p');
132
result.append('a');
133
result.append(' ');
134
result.append('a');
135
result.append('p');
136
result.append('a');
137
result.append(' ');
138
return result.toString();
139
}
140
141
@Benchmark
142
public String toStringCharWithChar16() {
143
StringBuilder result = new StringBuilder();
144
result.append('a');
145
result.append('b');
146
result.append('c');
147
result.append('d');
148
result.append('e');
149
result.append('f');
150
result.append('g');
151
result.append('h');
152
result.append('i');
153
result.append('j');
154
result.append('k');
155
result.append('l');
156
result.append('m');
157
result.append('n');
158
result.append('o');
159
result.append('p');
160
return result.toString();
161
}
162
163
164
@Benchmark
165
public String toStringCharWithString8() {
166
StringBuilder result = new StringBuilder();
167
result.append("a");
168
result.append("b");
169
result.append("c");
170
result.append("d");
171
result.append("e");
172
result.append("f");
173
result.append("g");
174
result.append("h");
175
return result.toString();
176
}
177
178
179
@Benchmark
180
public String toStringCharWithString16() {
181
StringBuilder result = new StringBuilder();
182
result.append("a");
183
result.append("b");
184
result.append("c");
185
result.append("d");
186
result.append("e");
187
result.append("f");
188
result.append("g");
189
result.append("h");
190
result.append("i");
191
result.append("j");
192
result.append("k");
193
result.append("l");
194
result.append("m");
195
result.append("n");
196
result.append("o");
197
result.append("p");
198
return result.toString();
199
}
200
201
202
@Benchmark
203
public String toStringCharWithInt8() {
204
StringBuilder result = new StringBuilder();
205
result.append(2048);
206
result.append(31337);
207
result.append(0xbeefcace);
208
result.append(9000);
209
result.append(4711);
210
result.append(1337);
211
result.append(2100);
212
result.append(2600);
213
return result.toString();
214
}
215
216
217
@Benchmark
218
public String toStringCharWithBool8() {
219
StringBuilder result = new StringBuilder();
220
result.append(true);
221
result.append(false);
222
result.append(true);
223
result.append(true);
224
result.append(false);
225
result.append(true);
226
result.append(false);
227
result.append(false);
228
return result.toString();
229
}
230
231
232
@Benchmark
233
public String toStringCharWithFloat8() {
234
StringBuilder result = new StringBuilder();
235
result.append(113.110F);
236
result.append(156456.36435637F);
237
result.append(65436434.64632F);
238
result.append(42654634.64540F);
239
result.append(63464351.64537F);
240
result.append(634564.645711F);
241
result.append(64547.64311F);
242
result.append(4763456341.64531F);
243
return result.toString();
244
}
245
246
247
@Benchmark
248
public String toStringCharWithMixed8() {
249
StringBuilder result = new StringBuilder();
250
result.append('a');
251
result.append("stringelinglinglinglong");
252
result.append('a');
253
result.append("stringelinglinglinglong");
254
result.append('a');
255
result.append("stringelinglinglinglong");
256
result.append('p');
257
result.append("stringelinglinglinglong");
258
return result.toString();
259
}
260
261
@Benchmark
262
public StringBuilder fromLatin1String() {
263
return new StringBuilder("Latin1 string");
264
}
265
266
@Benchmark
267
public StringBuilder fromUtf16String() {
268
return new StringBuilder("UTF-\uFF11\uFF16 string");
269
}
270
271
@Benchmark
272
public StringBuilder fromLatin1StringBuilder() {
273
return new StringBuilder(sbLatin1);
274
}
275
276
@Benchmark
277
public StringBuilder fromUtf16StringBuilder() {
278
return new StringBuilder(sbUtf16);
279
}
280
281
@Benchmark
282
@SuppressWarnings("StringBufferReplaceableByString")
283
public String appendSubstring(Data data) {
284
String str = data.str;
285
int beginIndex = data.beginIndex;
286
int endIndex = data.endIndex;
287
288
String substring = str.substring(beginIndex, endIndex);
289
return new StringBuilder().append('L').append(substring).append(';').toString();
290
}
291
292
@Benchmark
293
public String appendBounds(Data data) {
294
String str = data.str;
295
int beginIndex = data.beginIndex;
296
int endIndex = data.endIndex;
297
298
return new StringBuilder().append('L').append(str, beginIndex, endIndex).append(';').toString();
299
}
300
301
@Benchmark
302
@SuppressWarnings("StringBufferReplaceableByString")
303
public String appendSubstringUtf16(Data data) {
304
String str = data.utf16Str;
305
int beginIndex = data.beginIndex;
306
int endIndex = data.endIndex;
307
308
String substring = str.substring(beginIndex, endIndex);
309
310
return new StringBuilder().append('L').append(substring).append(';').toString();
311
}
312
313
@Benchmark
314
public String appendBoundsUtf16(Data data) {
315
String str = data.utf16Str;
316
int beginIndex = data.beginIndex;
317
int endIndex = data.endIndex;
318
319
return new StringBuilder().append('L').append(str, beginIndex,
320
endIndex).append(';').toString();
321
}
322
323
@Benchmark
324
public String appendBoundsMix(Data data) {
325
CharSequence str = data.next();
326
int beginIndex = data.beginIndex;
327
int endIndex = data.endIndex;
328
329
return new StringBuilder().append('L').append(str, beginIndex,
330
endIndex).append(';').toString();
331
}
332
333
@State(Scope.Thread)
334
public static class Data {
335
int i = 0;
336
337
public CharSequence next() {
338
i++;
339
if (i == 1) {
340
return str;
341
} else if (i == 2) {
342
return utf16Str;
343
} else {
344
i = 0;
345
return cs;
346
}
347
}
348
349
String str;
350
String utf16Str;
351
CharSequence cs;
352
353
@Param({"10", "1000"})
354
private int length;
355
356
private int beginIndex;
357
private int endIndex;
358
359
@Setup
360
public void setup() {
361
generateData();
362
beginIndex = length / 4;
363
endIndex = length / 4 * 3;
364
}
365
366
private void generateData() {
367
char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
368
369
StringBuilder sb = new StringBuilder(length);
370
for (int i = 0; i < length; i++) {
371
char c = chars[i % chars.length];
372
sb.append(c);
373
}
374
str = sb.toString();
375
sb.replace(length / 4 * 2, length / 4 * 2 + 1, "\u04FF");
376
utf16Str = sb.toString();
377
cs = new StringBuilder(str);
378
}
379
}
380
}
381
382