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.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 80 /** StringMaker wins over StringBuilder. */ 81 @Benchmark 82 public String concat22p40p31() throws Exception { 83 return new StringBuilder(String.valueOf(str22p40p31[0])).append(str22p40p31[1]).append(str22p40p31[2]).toString(); 84 } 85 86 @Benchmark 87 public StringBuilder appendLoop8() { 88 StringBuilder sb = new StringBuilder(); 89 for (int i = 0; i < 8; i++) { 90 sb.append(strings[i]); 91 } 92 return sb; 93 } 94 95 @Benchmark 96 public StringBuilder appendLoop16() { 97 StringBuilder sb = new StringBuilder(); 98 for (int i = 0; i < 16; i++) { 99 sb.append(strings[i]); 100 } 101 return sb; 102 } 103 104 @Benchmark 105 public String toStringCharWithChar1() { 106 StringBuilder result = new StringBuilder(); 107 result.append('a'); 108 return result.toString(); 109 } 110 111 @Benchmark 112 public String toStringCharWithChar2() { 113 StringBuilder result = new StringBuilder(); 114 result.append('a'); 115 result.append('p'); 116 return result.toString(); 117 } 118 119 120 @Benchmark 121 public String toStringCharWithChar4() { 122 StringBuilder result = new StringBuilder(); 123 result.append('a'); 124 result.append('p'); 125 result.append('a'); 126 result.append(' '); 127 return result.toString(); 128 } 129 130 @Benchmark 131 public String toStringCharWithChar8() { 132 StringBuilder result = new StringBuilder(); 133 result.append('a'); 134 result.append('p'); 135 result.append('a'); 136 result.append(' '); 137 result.append('a'); 138 result.append('p'); 139 result.append('a'); 140 result.append(' '); 141 return result.toString(); 142 } 143 144 @Benchmark 145 public String toStringCharWithChar16() { 146 StringBuilder result = new StringBuilder(); 147 result.append('a'); 148 result.append('b'); 149 result.append('c'); 150 result.append('d'); 151 result.append('e'); 152 result.append('f'); 153 result.append('g'); 154 result.append('h'); 155 result.append('i'); 156 result.append('j'); 157 result.append('k'); 158 result.append('l'); 159 result.append('m'); 160 result.append('n'); 161 result.append('o'); 162 result.append('p'); 163 return result.toString(); 164 } 165 166 167 @Benchmark 168 public String toStringCharWithString8() { 169 StringBuilder result = new StringBuilder(); 170 result.append("a"); 171 result.append("b"); 172 result.append("c"); 173 result.append("d"); 174 result.append("e"); 175 result.append("f"); 176 result.append("g"); 177 result.append("h"); 178 return result.toString(); 179 } 180 181 182 @Benchmark 183 public String toStringCharWithString16() { 184 StringBuilder result = new StringBuilder(); 185 result.append("a"); 186 result.append("b"); 187 result.append("c"); 188 result.append("d"); 189 result.append("e"); 190 result.append("f"); 191 result.append("g"); 192 result.append("h"); 193 result.append("i"); 194 result.append("j"); 195 result.append("k"); 196 result.append("l"); 197 result.append("m"); 198 result.append("n"); 199 result.append("o"); 200 result.append("p"); 201 return result.toString(); 202 } 203 204 205 @Benchmark 206 public String toStringCharWithInt8() { 207 StringBuilder result = new StringBuilder(); 208 result.append(2048); 209 result.append(31337); 210 result.append(0xbeefcace); 211 result.append(9000); 212 result.append(4711); 213 result.append(1337); 214 result.append(2100); 215 result.append(2600); 216 return result.toString(); 217 } 218 219 220 @Benchmark 221 public String toStringCharWithBool8() { 222 StringBuilder result = new StringBuilder(); 223 result.append(true); 224 result.append(false); 225 result.append(true); 226 result.append(true); 227 result.append(false); 228 result.append(true); 229 result.append(false); 230 result.append(false); 231 return result.toString(); 232 } 233 234 235 @Benchmark 236 public String toStringCharWithFloat8() { 237 StringBuilder result = new StringBuilder(); 238 result.append(113.110F); 239 result.append(156456.36435637F); 240 result.append(65436434.64632F); 241 result.append(42654634.64540F); 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 }