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 String[] str22p40p31;
  46     private StringBuilder sbLatin1;
  47     private StringBuilder sbUtf8;
  48 
  49         return new StringBuilder("Latin1 string");
  50     }
  51 
  52     @Setup
  53     public void setup() {
  54         strings = new String[]{"As", "your", "attorney,", "I",
  55                 "advise", "you", "to", "drive", "at", "top", "speed", "it'll",
  56                 "be", "a", "god", "damn", "miracle", "if", "we", "can", "get",
  57                 "there", "before", "you", "turn", "into", "a", "wild", "animal."};
  58         str3p4p2 = new String[]{"123", "1234", "12"};
  59         str16p8p7 = new String[]{"1234567890123456", "12345678", "1234567"};
  60         str3p9p8 = new String[]{"123", "123456789", "12345678"};
  61         str22p40p31 = new String[]{"1234567890123456789012", "1234567890123456789012345678901234567890", "1234567890123456789012345678901"};
  62         sbLatin1 = new StringBuilder("Latin1 string");
  63         sbUtf8 = new StringBuilder("UTF-\uFF18 string");
  64     }
  65 
  66     /** StringBuilder wins over StringMaker. */
  67     @Benchmark
  68     public String concat3p4p2() throws Exception {
  69         return new StringBuilder(String.valueOf(str3p4p2[0])).append(str3p4p2[1]).append(str3p4p2[2]).toString();
  70     }
  71 
  72     /** StringBuilder wins over StringMaker. */
  73     @Benchmark
  74     public String concat16p8p7() throws Exception {
  75         return new StringBuilder(String.valueOf(str16p8p7[0])).append(str16p8p7[1]).append(str16p8p7[2]).toString();
  76     }
  77 
  78     /** StringMaker wins over StringBuilder since the two last strings causes StringBuilder to do expand. */
  79     @Benchmark
  80     public String concat3p9p8() throws Exception {
  81         return new StringBuilder(String.valueOf(str3p9p8[0])).append(str3p9p8[1]).append(str3p9p8[2]).toString();
  82     }
  83 
  84     /** StringMaker wins over StringBuilder. */
  85     @Benchmark
  86     public String concat22p40p31() throws Exception {
  87         return new StringBuilder(String.valueOf(str22p40p31[0])).append(str22p40p31[1]).append(str22p40p31[2]).toString();
  88     }
  89 
  90     @Benchmark
  91     public StringBuilder appendLoop8() {
  92         StringBuilder sb = new StringBuilder();
  93         for (int i = 0; i < 8; i++) {
  94             sb.append(strings[i]);
  95         }
  96         return sb;
  97     }
  98 
  99     @Benchmark
 100     public StringBuilder appendLoop16() {
 101         StringBuilder sb = new StringBuilder();
 102         for (int i = 0; i < 16; i++) {
 103             sb.append(strings[i]);
 104         }
 105         return sb;
 106     }
 107 
 108     @Benchmark
 109     public String toStringCharWithChar1() {
 110         StringBuilder result = new StringBuilder();
 111         result.append('a');
 112         return result.toString();
 113     }
 114 
 115     @Benchmark
 116     public String toStringCharWithChar2() {
 117         StringBuilder result = new StringBuilder();
 118         result.append('a');
 119         result.append('p');
 120         return result.toString();
 121     }
 122 
 123 
 124     @Benchmark
 125     public String toStringCharWithChar4() {
 126         StringBuilder result = new StringBuilder();
 127         result.append('a');
 128         result.append('p');
 129         result.append('a');
 130         result.append(' ');
 131         return result.toString();
 132     }
 133 
 134     @Benchmark
 135     public String toStringCharWithChar8() {
 136         StringBuilder result = new StringBuilder();
 137         result.append('a');
 138         result.append('p');
 139         result.append('a');
 140         result.append(' ');
 141         result.append('a');
 142         result.append('p');
 143         result.append('a');
 144         result.append(' ');
 145         return result.toString();
 146     }
 147 
 148     @Benchmark
 149     public String toStringCharWithChar16() {
 150         StringBuilder result = new StringBuilder();
 151         result.append('a');
 152         result.append('b');
 153         result.append('c');
 154         result.append('d');
 155         result.append('e');
 156         result.append('f');
 157         result.append('g');
 158         result.append('h');
 159         result.append('i');
 160         result.append('j');
 161         result.append('k');
 162         result.append('l');
 163         result.append('m');
 164         result.append('n');
 165         result.append('o');
 166         result.append('p');
 167         return result.toString();
 168     }
 169 
 170 
 171     @Benchmark
 172     public String toStringCharWithString8() {
 173         StringBuilder result = new StringBuilder();
 174         result.append("a");
 175         result.append("b");
 176         result.append("c");
 177         result.append("d");
 178         result.append("e");
 179         result.append("f");
 180         result.append("g");
 181         result.append("h");
 182         return result.toString();
 183     }
 184 
 185 
 186     @Benchmark
 187     public String toStringCharWithString16() {
 188         StringBuilder result = new StringBuilder();
 189         result.append("a");
 190         result.append("b");
 191         result.append("c");
 192         result.append("d");
 193         result.append("e");
 194         result.append("f");
 195         result.append("g");
 196         result.append("h");
 197         result.append("i");
 198         result.append("j");
 199         result.append("k");
 200         result.append("l");
 201         result.append("m");
 202         result.append("n");
 203         result.append("o");
 204         result.append("p");
 205         return result.toString();
 206     }
 207 
 208 
 209     @Benchmark
 210     public String toStringCharWithInt8() {
 211         StringBuilder result = new StringBuilder();
 212         result.append(2048);
 213         result.append(31337);
 214         result.append(0xbeefcace);
 215         result.append(9000);
 216         result.append(4711);
 217         result.append(1337);
 218         result.append(2100);
 219         result.append(2600);
 220         return result.toString();
 221     }
 222 
 223 
 224     @Benchmark
 225     public String toStringCharWithBool8() {
 226         StringBuilder result = new StringBuilder();
 227         result.append(true);
 228         result.append(false);
 229         result.append(true);
 230         result.append(true);
 231         result.append(false);
 232         result.append(true);
 233         result.append(false);
 234         result.append(false);
 235         return result.toString();
 236     }
 237 
 238 
 239     @Benchmark
 240     public String toStringCharWithFloat8() {
 241         StringBuilder result = new StringBuilder();
 242         result.append(113.110F);
 243         result.append(156456.36435637F);
 244         result.append(65436434.64632F);
 245         result.append(42654634.64540F);
 246         result.append(63464351.64537F);
 247         result.append(634564.645711F);
 248         result.append(64547.64311F);
 249         result.append(4763456341.64531F);
 250         return result.toString();
 251     }
 252 
 253 
 254     @Benchmark
 255     public String toStringCharWithMixed8() {
 256         StringBuilder result = new StringBuilder();
 257         result.append('a');
 258         result.append("stringelinglinglinglong");
 259         result.append('a');
 260         result.append("stringelinglinglinglong");
 261         result.append('a');
 262         result.append("stringelinglinglinglong");
 263         result.append('p');
 264         result.append("stringelinglinglinglong");
 265         return result.toString();
 266     }
 267 
 268     @Benchmark
 269     public StringBuilder fromLatin1String() {
 270         return new StringBuilder("Latin1 string");
 271     }
 272 
 273     @Benchmark
 274     public StringBuilder fromUtf8String() {
 275         return new StringBuilder("UTF-\uFF18 string");
 276     }
 277 
 278     @Benchmark
 279     public StringBuilder fromLatin1StringBuilder() {
 280         return new StringBuilder(sbLatin1);
 281     }
 282 
 283     @Benchmark
 284     public StringBuilder fromUtf8StringBuilder() {
 285         return new StringBuilder(sbUtf8);
 286     }
 287 }