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