Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.example.benchmarks;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.StringJoiner;
- import java.util.concurrent.TimeUnit;
- import org.openjdk.jmh.annotations.Benchmark;
- import org.openjdk.jmh.annotations.BenchmarkMode;
- import org.openjdk.jmh.annotations.Mode;
- import org.openjdk.jmh.annotations.OperationsPerInvocation;
- import org.openjdk.jmh.annotations.OutputTimeUnit;
- import org.openjdk.jmh.runner.Runner;
- import org.openjdk.jmh.runner.RunnerException;
- import org.openjdk.jmh.runner.options.Options;
- import org.openjdk.jmh.runner.options.OptionsBuilder;
- import com.google.common.base.Joiner;
- @OutputTimeUnit(TimeUnit.NANOSECONDS)
- @BenchmarkMode(Mode.AverageTime)
- @OperationsPerInvocation(MyBenchmark.LIST_SIZE)
- public class MyBenchmark {
- private static final char SEPARATOR = '_';
- // launch from maven with: clean install exec:exec
- public static void main(String[] args) throws RunnerException {
- Options opt = new OptionsBuilder()
- .include(".*" + MyBenchmark.class.getSimpleName() + ".*")
- .forks(1).build();
- new Runner(opt).run();
- }
- public static final int LIST_SIZE = 20000;
- static List<String> sourceList = new ArrayList<>();
- static {
- for (int i = 0; i < LIST_SIZE; i++) {
- sourceList.add("item" + i);
- }
- }
- @Benchmark
- public String concatenationUsingPlus() {
- // Considered bad practice since the dawn of Java
- String result = "";
- for (String s : sourceList) {
- if (result.length() > 0) {
- result += SEPARATOR;
- }
- result += s;
- }
- return result;
- }
- @Benchmark
- public String concatenationUsingStringBuffer() {
- // Considered slow because of synchronization
- StringBuffer result = new StringBuffer();
- for (String s : sourceList) {
- if (result.length() > 0) {
- result.append(SEPARATOR);
- }
- result.append(s);
- }
- return result.toString();
- }
- @Benchmark
- public String concatenationUsingStringBuilder() {
- // The approved pre-Java-8 approach
- StringBuilder result = new StringBuilder();
- for (String s : sourceList) {
- if (result.length() > 0) {
- result.append(SEPARATOR);
- }
- result.append(s);
- }
- return result.toString();
- }
- @Benchmark
- public String concatenationUsingGuavaJoiner() {
- // A simple and elegant approach, available pre-Java-8, but with a
- // 3rd-party dependency
- return Joiner.on(SEPARATOR).join(sourceList);
- }
- @Benchmark
- public String concatenationUsingStringJoinerOnForLoop() {
- // A Java-8 approach
- StringJoiner result = new StringJoiner("" + SEPARATOR);
- for (String s : sourceList) {
- result.add(s);
- }
- return result.toString();
- }
- @Benchmark
- public String concatenationUsingStringJoinerInStreamWithForEachOrdered() {
- // A Java-8 approach
- StringJoiner result = new StringJoiner("" + SEPARATOR);
- sourceList.stream().forEachOrdered(s -> result.add(s));
- return result.toString();
- }
- @Benchmark
- public String concatenationUsingStringJoin() {
- // The simplest, most concise Java-8 approach
- return String.join("" + SEPARATOR, sourceList);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement