Benchmark , Performance StringJoiner StringBuffer “+” plus operator

This is a benchmark for performance and behaviour of joining strings with:

  • StringJoiner
  • StringBuffer
  • The + operator

The result is that

StringJoiner is the winner.

StringBuffer and ‘+’ are worse and have the same performance.

package org.sample;
 
import java.util.ArrayList;
import java.util.List;
 
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
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;
 
@Warmup(iterations = 10, time = 1)
@Measurement(iterations = 10, time = 1)
@State(Scope.Benchmark)
@Fork(1)
public class JoinString extends JoinStringBase {
	@Param({ "10000" })
	private int CYCLES;
 
	private final int P = 4;
 
	private List listCounter;
 
	private List listA;
	private List listC;
	private List listD;
	private List listXP;
	private List listJOINER;
 
	@Setup(Level.Trial)
	public void setUp() {
		listCounter = new ArrayList<>(CYCLES + 1);
 
		for (int i = 0; i < CYCLES; i++) {
			listCounter.add("" + i);
		}
 
		listA = new ArrayList<>(CYCLES + 1);
		listC = new ArrayList<>(CYCLES + 1);
		listD = new ArrayList<>(CYCLES + 1);
		listXP = new ArrayList<>(CYCLES + 1);
		listJOINER = new ArrayList<>(CYCLES + 1);
 
		System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", Integer.toString(P));
	}
 
	@Benchmark
	public long useStringBuffer() {
		for (int i = 0; i < CYCLES; i++) {
			listC.add(bufferDateString(TEXT));
		}
		return listC.size();
	}
 
	@Benchmark
	public long plusOperator() {
		for (int i = 0; i < CYCLES; i++) {
			listA.add(plusOperator(TEXT));
		}
		return listA.size();
	}
 
	@Benchmark
	public long useStringJoiner() {
		for (int i = 0; i < CYCLES; i++) {
			listJOINER.add(stringJoiner(TEXT));
		}
		return listJOINER.size();
	}
}
 
 
 
import java.util.StringJoiner;
 
class JoinStringBase {
 
	private static final String TWENTY = "20";
	private static final String POINT = ".";
 
	protected static final String TEXT = "_20ge20";
 
	String stringJoiner(String pScad) {
		StringJoiner sj = new StringJoiner("");
		String day = pScad.substring(1, 3);
		String month = pScad.substring(3, 5);
		String monthInt = MONTHS.valueOf(month).month;
		String yearSuffix = pScad.substring(5, 7);
 
		sj.add(day);
		sj.add(POINT);
		sj.add(monthInt);
		sj.add(POINT);
		sj.add(TWENTY);
		sj.add(yearSuffix);
		return sj.toString();
 
	}
 
	final String bufferDateString(String pScad) {
		StringBuffer buffer = new StringBuffer();
		String day = pScad.substring(1, 3);
		String month = pScad.substring(3, 5);
		String monthInt = MONTHS.valueOf(month).month;
		String yearSuffix = pScad.substring(5, 7);
 
		buffer.append(day);
		buffer.append(POINT);
 
		buffer.append(monthInt);
		buffer.append(POINT);
 
		buffer.append(TWENTY);
		buffer.append(yearSuffix);
		return buffer.toString();
	}
 
	final String plusOperator(String pScad) {
		String day = pScad.substring(1, 3);
		String month = pScad.substring(3, 5);
		String monthInt = MONTHS.valueOf(month).month;
		String yearSuffix = pScad.substring(5, 7);
 
		String scadFormat = day + POINT + monthInt + POINT + TWENTY + yearSuffix;
		return scadFormat;
	}
 
	enum MONTHS {
		ge("01"), fb("02"), mz("03"), ap("04"), mg("05"), gn("06"), lg("07"), ag("08"), st("09"), ot("10"), nv("11"), dc("12");
 
		final String month;
 
		MONTHS(String month) {
			this.month = month;
		}
	}
 
	final String plusOperatorXP(String pScad) {
		return pScad.substring(1, 3) + POINT + MONTHS.valueOf(pScad.substring(3, 5)).month + POINT + TWENTY
				+ pScad.substring(5, 7);
	}
 
	final String bufferDateStringXP(String pScad) {
		StringBuffer buffer = new StringBuffer();
 
		String month = pScad.substring(3, 5);
 
		buffer.append(pScad.substring(1, 3));
		buffer.append(POINT);
 
		buffer.append(MONTHS.valueOf(month).month);
		buffer.append(POINT);
 
		buffer.append(TWENTY);
		buffer.append(pScad.substring(5, 7));
		return buffer.toString();
	}
 
}

 

The results in details:

blogJoinString2