Path: blob/master/test/micro/org/openjdk/bench/java/lang/StringBuilders.java
41161 views
/*1* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.java.lang;2324import org.openjdk.jmh.annotations.Benchmark;25import org.openjdk.jmh.annotations.BenchmarkMode;26import org.openjdk.jmh.annotations.Mode;27import org.openjdk.jmh.annotations.OutputTimeUnit;28import org.openjdk.jmh.annotations.Param;29import org.openjdk.jmh.annotations.Scope;30import org.openjdk.jmh.annotations.Setup;31import org.openjdk.jmh.annotations.State;3233import java.util.concurrent.TimeUnit;3435@BenchmarkMode(Mode.AverageTime)36@OutputTimeUnit(TimeUnit.NANOSECONDS)37@State(Scope.Thread)38public class StringBuilders {3940private String[] strings;41private String[] str3p4p2;42private String[] str16p8p7;43private String[] str3p9p8;44private String[] str22p40p31;45private StringBuilder sbLatin1;46private StringBuilder sbUtf16;4748@Setup49public void setup() {50strings = 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."};54str3p4p2 = new String[]{"123", "1234", "12"};55str16p8p7 = new String[]{"1234567890123456", "12345678", "1234567"};56str3p9p8 = new String[]{"123", "123456789", "12345678"};57str22p40p31 = new String[]{"1234567890123456789012", "1234567890123456789012345678901234567890", "1234567890123456789012345678901"};58sbLatin1 = new StringBuilder("Latin1 string");59sbUtf16 = new StringBuilder("UTF-\uFF11\uFF16 string");60}6162@Benchmark63public String concat3p4p2() throws Exception {64return new StringBuilder(String.valueOf(str3p4p2[0])).append(str3p4p2[1]).append(str3p4p2[2]).toString();65}6667@Benchmark68public String concat16p8p7() throws Exception {69return new StringBuilder(String.valueOf(str16p8p7[0])).append(str16p8p7[1]).append(str16p8p7[2]).toString();70}7172@Benchmark73public String concat3p9p8() throws Exception {74return new StringBuilder(String.valueOf(str3p9p8[0])).append(str3p9p8[1]).append(str3p9p8[2]).toString();75}7677@Benchmark78public String concat22p40p31() throws Exception {79return new StringBuilder(String.valueOf(str22p40p31[0])).append(str22p40p31[1]).append(str22p40p31[2]).toString();80}8182@Benchmark83public StringBuilder appendLoop8() {84StringBuilder sb = new StringBuilder();85for (int i = 0; i < 8; i++) {86sb.append(strings[i]);87}88return sb;89}9091@Benchmark92public StringBuilder appendLoop16() {93StringBuilder sb = new StringBuilder();94for (int i = 0; i < 16; i++) {95sb.append(strings[i]);96}97return sb;98}99100@Benchmark101public String toStringCharWithChar1() {102StringBuilder result = new StringBuilder();103result.append('a');104return result.toString();105}106107@Benchmark108public String toStringCharWithChar2() {109StringBuilder result = new StringBuilder();110result.append('a');111result.append('p');112return result.toString();113}114115116@Benchmark117public String toStringCharWithChar4() {118StringBuilder result = new StringBuilder();119result.append('a');120result.append('p');121result.append('a');122result.append(' ');123return result.toString();124}125126@Benchmark127public String toStringCharWithChar8() {128StringBuilder result = new StringBuilder();129result.append('a');130result.append('p');131result.append('a');132result.append(' ');133result.append('a');134result.append('p');135result.append('a');136result.append(' ');137return result.toString();138}139140@Benchmark141public String toStringCharWithChar16() {142StringBuilder result = new StringBuilder();143result.append('a');144result.append('b');145result.append('c');146result.append('d');147result.append('e');148result.append('f');149result.append('g');150result.append('h');151result.append('i');152result.append('j');153result.append('k');154result.append('l');155result.append('m');156result.append('n');157result.append('o');158result.append('p');159return result.toString();160}161162163@Benchmark164public String toStringCharWithString8() {165StringBuilder result = new StringBuilder();166result.append("a");167result.append("b");168result.append("c");169result.append("d");170result.append("e");171result.append("f");172result.append("g");173result.append("h");174return result.toString();175}176177178@Benchmark179public String toStringCharWithString16() {180StringBuilder result = new StringBuilder();181result.append("a");182result.append("b");183result.append("c");184result.append("d");185result.append("e");186result.append("f");187result.append("g");188result.append("h");189result.append("i");190result.append("j");191result.append("k");192result.append("l");193result.append("m");194result.append("n");195result.append("o");196result.append("p");197return result.toString();198}199200201@Benchmark202public String toStringCharWithInt8() {203StringBuilder result = new StringBuilder();204result.append(2048);205result.append(31337);206result.append(0xbeefcace);207result.append(9000);208result.append(4711);209result.append(1337);210result.append(2100);211result.append(2600);212return result.toString();213}214215216@Benchmark217public String toStringCharWithBool8() {218StringBuilder result = new StringBuilder();219result.append(true);220result.append(false);221result.append(true);222result.append(true);223result.append(false);224result.append(true);225result.append(false);226result.append(false);227return result.toString();228}229230231@Benchmark232public String toStringCharWithFloat8() {233StringBuilder result = new StringBuilder();234result.append(113.110F);235result.append(156456.36435637F);236result.append(65436434.64632F);237result.append(42654634.64540F);238result.append(63464351.64537F);239result.append(634564.645711F);240result.append(64547.64311F);241result.append(4763456341.64531F);242return result.toString();243}244245246@Benchmark247public String toStringCharWithMixed8() {248StringBuilder result = new StringBuilder();249result.append('a');250result.append("stringelinglinglinglong");251result.append('a');252result.append("stringelinglinglinglong");253result.append('a');254result.append("stringelinglinglinglong");255result.append('p');256result.append("stringelinglinglinglong");257return result.toString();258}259260@Benchmark261public StringBuilder fromLatin1String() {262return new StringBuilder("Latin1 string");263}264265@Benchmark266public StringBuilder fromUtf16String() {267return new StringBuilder("UTF-\uFF11\uFF16 string");268}269270@Benchmark271public StringBuilder fromLatin1StringBuilder() {272return new StringBuilder(sbLatin1);273}274275@Benchmark276public StringBuilder fromUtf16StringBuilder() {277return new StringBuilder(sbUtf16);278}279280@Benchmark281@SuppressWarnings("StringBufferReplaceableByString")282public String appendSubstring(Data data) {283String str = data.str;284int beginIndex = data.beginIndex;285int endIndex = data.endIndex;286287String substring = str.substring(beginIndex, endIndex);288return new StringBuilder().append('L').append(substring).append(';').toString();289}290291@Benchmark292public String appendBounds(Data data) {293String str = data.str;294int beginIndex = data.beginIndex;295int endIndex = data.endIndex;296297return new StringBuilder().append('L').append(str, beginIndex, endIndex).append(';').toString();298}299300@Benchmark301@SuppressWarnings("StringBufferReplaceableByString")302public String appendSubstringUtf16(Data data) {303String str = data.utf16Str;304int beginIndex = data.beginIndex;305int endIndex = data.endIndex;306307String substring = str.substring(beginIndex, endIndex);308309return new StringBuilder().append('L').append(substring).append(';').toString();310}311312@Benchmark313public String appendBoundsUtf16(Data data) {314String str = data.utf16Str;315int beginIndex = data.beginIndex;316int endIndex = data.endIndex;317318return new StringBuilder().append('L').append(str, beginIndex,319endIndex).append(';').toString();320}321322@Benchmark323public String appendBoundsMix(Data data) {324CharSequence str = data.next();325int beginIndex = data.beginIndex;326int endIndex = data.endIndex;327328return new StringBuilder().append('L').append(str, beginIndex,329endIndex).append(';').toString();330}331332@State(Scope.Thread)333public static class Data {334int i = 0;335336public CharSequence next() {337i++;338if (i == 1) {339return str;340} else if (i == 2) {341return utf16Str;342} else {343i = 0;344return cs;345}346}347348String str;349String utf16Str;350CharSequence cs;351352@Param({"10", "1000"})353private int length;354355private int beginIndex;356private int endIndex;357358@Setup359public void setup() {360generateData();361beginIndex = length / 4;362endIndex = length / 4 * 3;363}364365private void generateData() {366char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();367368StringBuilder sb = new StringBuilder(length);369for (int i = 0; i < length; i++) {370char c = chars[i % chars.length];371sb.append(c);372}373str = sb.toString();374sb.replace(length / 4 * 2, length / 4 * 2 + 1, "\u04FF");375utf16Str = sb.toString();376cs = new StringBuilder(str);377}378}379}380381382