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 */
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.Scope;
30 import org.openjdk.jmh.annotations.Setup;
31 import org.openjdk.jmh.annotations.State;
32
33 import java.util.concurrent.TimeUnit;
34
35 @BenchmarkMode(Mode.AverageTime)
36 @OutputTimeUnit(TimeUnit.NANOSECONDS)
37 @State(Scope.Thread)
38 public class StringBuilders {
39
40 private String[] strings;
41 private String[] str3p4p2;
42 private String[] str16p8p7;
43 private String[] str3p9p8;
44 private String[] str22p40p31;
45 private StringBuilder sbLatin1;
46 private StringBuilder sbUtf16;
47
48 @Setup
49 public void setup() {
50 strings = new String[]{"As", "your", "attorney,", "I",
51 "advise", "you", "to", "drive", "at", "top", "speed", "it'll",
52 "be", "a", "god", "damn", "miracle", "if", "we", "can", "get",
53 "there", "before", "you", "turn", "into", "a", "wild", "animal."};
54 str3p4p2 = new String[]{"123", "1234", "12"};
55 str16p8p7 = new String[]{"1234567890123456", "12345678", "1234567"};
56 str3p9p8 = new String[]{"123", "123456789", "12345678"};
57 str22p40p31 = new String[]{"1234567890123456789012", "1234567890123456789012345678901234567890", "1234567890123456789012345678901"};
58 sbLatin1 = new StringBuilder("Latin1 string");
59 sbUtf16 = new StringBuilder("UTF-\uFF11\uFF16 string");
60 }
61
62 /** StringBuilder wins over StringMaker. */
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 /** StringBuilder wins over StringMaker. */
69 @Benchmark
70 public String concat16p8p7() throws Exception {
71 return new StringBuilder(String.valueOf(str16p8p7[0])).append(str16p8p7[1]).append(str16p8p7[2]).toString();
72 }
73
74 /** StringMaker wins over StringBuilder since the two last strings causes StringBuilder to do expand. */
75 @Benchmark
76 public String concat3p9p8() throws Exception {
77 return new StringBuilder(String.valueOf(str3p9p8[0])).append(str3p9p8[1]).append(str3p9p8[2]).toString();
78 }
79
242 result.append(63464351.64537F);
243 result.append(634564.645711F);
244 result.append(64547.64311F);
245 result.append(4763456341.64531F);
246 return result.toString();
247 }
248
249
250 @Benchmark
251 public String toStringCharWithMixed8() {
252 StringBuilder result = new StringBuilder();
253 result.append('a');
254 result.append("stringelinglinglinglong");
255 result.append('a');
256 result.append("stringelinglinglinglong");
257 result.append('a');
258 result.append("stringelinglinglinglong");
259 result.append('p');
260 result.append("stringelinglinglinglong");
261 return result.toString();
262 }
263
264 @Benchmark
265 public StringBuilder fromLatin1String() {
266 return new StringBuilder("Latin1 string");
267 }
268
269 @Benchmark
270 public StringBuilder fromUtf16String() {
271 return new StringBuilder("UTF-\uFF11\uFF16 string");
272 }
273
274 @Benchmark
275 public StringBuilder fromLatin1StringBuilder() {
276 return new StringBuilder(sbLatin1);
277 }
278
279 @Benchmark
280 public StringBuilder fromUtf16StringBuilder() {
281 return new StringBuilder(sbUtf16);
282 }
283 }
|
1 /*
2 * Copyright (c) 2014 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 */
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.Scope;
30 import org.openjdk.jmh.annotations.Setup;
31 import org.openjdk.jmh.annotations.State;
32
33 import java.util.concurrent.TimeUnit;
34
35 @BenchmarkMode(Mode.AverageTime)
36 @OutputTimeUnit(TimeUnit.NANOSECONDS)
37 @State(Scope.Thread)
38 public class StringBuilders {
39
40 private String[] strings;
41 private String[] str3p4p2;
42 private String[] str16p8p7;
43 private String[] str3p9p8;
44 private String[] str22p40p31;
45
46 @Setup
47 public void setup() {
48 strings = new String[]{"As", "your", "attorney,", "I",
49 "advise", "you", "to", "drive", "at", "top", "speed", "it'll",
50 "be", "a", "god", "damn", "miracle", "if", "we", "can", "get",
51 "there", "before", "you", "turn", "into", "a", "wild", "animal."};
52 str3p4p2 = new String[]{"123", "1234", "12"};
53 str16p8p7 = new String[]{"1234567890123456", "12345678", "1234567"};
54 str3p9p8 = new String[]{"123", "123456789", "12345678"};
55 str22p40p31 = new String[]{"1234567890123456789012", "1234567890123456789012345678901234567890", "1234567890123456789012345678901"};
56 }
57
58 /** StringBuilder wins over StringMaker. */
59 @Benchmark
60 public String concat3p4p2() throws Exception {
61 return new StringBuilder(String.valueOf(str3p4p2[0])).append(str3p4p2[1]).append(str3p4p2[2]).toString();
62 }
63
64 /** StringBuilder wins over StringMaker. */
65 @Benchmark
66 public String concat16p8p7() throws Exception {
67 return new StringBuilder(String.valueOf(str16p8p7[0])).append(str16p8p7[1]).append(str16p8p7[2]).toString();
68 }
69
70 /** StringMaker wins over StringBuilder since the two last strings causes StringBuilder to do expand. */
71 @Benchmark
72 public String concat3p9p8() throws Exception {
73 return new StringBuilder(String.valueOf(str3p9p8[0])).append(str3p9p8[1]).append(str3p9p8[2]).toString();
74 }
75
238 result.append(63464351.64537F);
239 result.append(634564.645711F);
240 result.append(64547.64311F);
241 result.append(4763456341.64531F);
242 return result.toString();
243 }
244
245
246 @Benchmark
247 public String toStringCharWithMixed8() {
248 StringBuilder result = new StringBuilder();
249 result.append('a');
250 result.append("stringelinglinglinglong");
251 result.append('a');
252 result.append("stringelinglinglinglong");
253 result.append('a');
254 result.append("stringelinglinglinglong");
255 result.append('p');
256 result.append("stringelinglinglinglong");
257 return result.toString();
258 }
259 }
|