Skip to content

Instantly share code, notes, and snippets.

@plevart
Created April 14, 2021 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plevart/86ac7fc6d4541dbc08256cde544019ce to your computer and use it in GitHub Desktop.
Save plevart/86ac7fc6d4541dbc08256cde544019ce to your computer and use it in GitHub Desktop.
package si.pele.jmh;
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import java.util.SplittableRandom;
import java.util.StringJoiner;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
@Fork(1)
public class StringJoinBench {
static final String utf16chars = "ČŠŽĐĆ";
@Param("10")
int numElem;
@Param({"10", "20", "100", "1000"})
int elemLength;
@Param({"LATIN1", "UTF16"})
Coder coder;
public enum Coder {
LATIN1, UTF16
}
private String[] elements;
@Setup
public void setup() {
var rnd = new SplittableRandom(0L); // deterministic
elements = new String[numElem];
for (int i = 0; i < numElem; i++) {
StringBuilder sb = new StringBuilder(elemLength);
for (int j = 0; j < elemLength; j++) {
char l1char = (char) rnd.nextInt(' ', '~');
char utf16char = utf16chars.charAt(rnd.nextInt(utf16chars.length()));
sb.append(coder == Coder.UTF16 &&
(j + 1 == elemLength || rnd.nextInt(10) == 0)
? utf16char
: l1char);
}
elements[i] = sb.toString();
}
}
@Benchmark
public String join() {
return String.join(",", elements);
}
@Benchmark
public String stringJoiner() {
var sj = new StringJoiner(",", "[", "]");
for (var s : elements) {
sj.add(s);
}
return sj.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment