Path: blob/master/test/micro/org/openjdk/bench/java/util/StringJoinerBenchmark.java
41161 views
/*1* Copyright (c) 2018, 2021, 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.util;2324import org.openjdk.jmh.annotations.Benchmark;25import org.openjdk.jmh.annotations.BenchmarkMode;26import org.openjdk.jmh.annotations.Fork;27import org.openjdk.jmh.annotations.Measurement;28import org.openjdk.jmh.annotations.Mode;29import org.openjdk.jmh.annotations.OutputTimeUnit;30import org.openjdk.jmh.annotations.Param;31import org.openjdk.jmh.annotations.Scope;32import org.openjdk.jmh.annotations.Setup;33import org.openjdk.jmh.annotations.State;34import org.openjdk.jmh.annotations.Warmup;3536import java.util.StringJoiner;37import java.util.concurrent.ThreadLocalRandom;38import java.util.concurrent.TimeUnit;3940/**41* Trivial benchmark for String joining with {@link java.util.StringJoiner}.42*/43@BenchmarkMode(Mode.AverageTime)44@OutputTimeUnit(TimeUnit.NANOSECONDS)45@Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)46@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)47@Fork(value = 3, jvmArgsAppend = {"-Xms1g", "-Xmx1g"})48public class StringJoinerBenchmark {4950@Benchmark51public String join(Data data) {52String[] stringArray = data.stringArray;53return String.join(",", stringArray);54}5556@Benchmark57public String stringJoiner(Data data) {58String[] stringArray = data.stringArray;59return Joiner.joinWithStringJoiner(stringArray);60}6162@State(Scope.Thread)63public static class Data {6465@Param({"latin", "cyrillic"})66private String mode;6768@Param({"1", "8", "32", "128"})69private int length;7071@Param({"5", "20"})72private int count;7374private String[] stringArray;7576@Setup77public void setup() {78stringArray = new String[count];7980for (int i = 0; i < count; i++) {81String alphabet = getAlphabet(i, mode);82stringArray[i] = randomString(alphabet, length);83}84}8586private String randomString(String alphabet, int length) {87var tl = ThreadLocalRandom.current();88StringBuilder sb = new StringBuilder();89for (int i = 0; i < length; i++) {90sb.append(alphabet.charAt(tl.nextInt(alphabet.length())));91}92return sb.toString();93}9495private static String getAlphabet(int index, String mode) {96var latin = "abcdefghijklmnopqrstuvwxyz"; //English97StringBuilder sb = new StringBuilder();98latin.codePoints().forEach(cp -> sb.append(cp - 'a' + '\u0430'));99var cyrillic = sb.toString(); // Russian (partial, matching length of latin alphabet)100101String alphabet;102switch (mode) {103case "latin" -> alphabet = latin;104case "cyrillic" -> alphabet = cyrillic;105default -> throw new RuntimeException("Illegal mode " + mode);106}107return alphabet;108}109}110}111112class Joiner {113public static String joinWithStringJoiner(String[] stringArray) {114StringJoiner joiner = new StringJoiner(",", "[", "]");115for (String str : stringArray) {116joiner.add(str);117}118return joiner.toString();119}120}121122123