Is String # format (String, Object ...) really slow?

Introduction

--In conclusion, it's certainly slower than string concatenation --However, it is often not appropriate to replace the format string with string concatenation. Especially when the number of parameters is large or when it is necessary to stringify the parameters. --In the first place, from the purpose of "replace placeholders in strings", you should compare __java.text.MessageFormat # format (String, Object ...) __, __StringBuilder # append (...) Not a method chain of __ --So, let's compare the execution speed of __String # format (String, Object ...) __ and __MessageFormat # format (String, Object ...) __. (benchmark1) --In addition, a comparison that retains the replaced character string is also performed. Comparison of __StringBuilder + String # format (String, Object ...) __ and __java.util.Formatter # format (String, Object ...) __ (benchmark2)

environment

    |version|
---+----------+
OS | macOS 10.14.6
Java | 1.8.0.202

benchmark

--Permutation processing of 2 character strings and replacement processing of 5 numerical values are performed 1 million times, and the average value is calculated.

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

class FormatPerformance {
	/**
	 *Number of loops(1 million times)
	 */
	private final int LOOP = 1000 * 1000;
	private static final ThreadLocalRandom random = ThreadLocalRandom.current();

	public static void main(String[] args) {
		FormatPerformance fp = new FormatPerformance();

		System.out.println("===== benchmark1 =====");
		fp.benchmark1();
		System.out.println("");
		System.out.println("===== benchmark2 =====");
		fp.benchmark2();
	}

	/**
	 *Returns a random string
	 * 
	 * @return Random alphabet string
	 */
	private String randomString() {
		final int size = random.nextInt(16) + 1;
		StringBuilder sb = new StringBuilder(size);
		for (int i = 0; i < size; i++) {
			sb.append((char)('a' + random.nextInt(26)));
		}
		return sb.toString();
	}

	/**
	 *Format conversion bench<br>
	 * {@link String#format(String, Object...)} vs {@link MessageFormat#format(String, Object...)}
	 */
	private void benchmark1() {
		final String[][] expects = { { "great!%s is%I'm a friend who is good at s!", "IP address%d.%d.%d.%d/%d" },
				{ "great!{0}Is{1}I'm a friend who is good at!", "IP address{0}.{1}.{2}.{3}/{4}" }, };

		long totalByConcat = 0;
		long totalByString = 0;
		long totalByMessage = 0;
		for (int i = 0; i < LOOP; i++) {
			String paramStr1 = randomString();
			String paramStr2 = randomString();
			int paramInt1 = random.nextInt(255);
			int paramInt2 = random.nextInt(255);
			int paramInt3 = random.nextInt(255);
			int paramInt4 = random.nextInt(255);
			int paramInt5 = random.nextInt(32);
			List<String> list = new ArrayList<>();

			//String concatenation
			{
				long begin = System.nanoTime();
				String sugoi = "great!" + paramStr1 + "Is" + paramStr2 + "I'm a friend who is good at!";
				String ipAddress = "IP address" + Integer.toString(paramInt1) + "." + Integer.toString(paramInt2) + "."
						+ Integer.toString(paramInt3) + "." + Integer.toString(paramInt4) + "/"
						+ Integer.toString(paramInt5);
				totalByConcat += System.nanoTime() - begin;
				list.add(sugoi);
				list.add(ipAddress);
			}

			// String#format(String, Object...)
			{
				long begin = System.nanoTime();
				String sugoi = String.format(expects[0][0], paramStr1, paramStr2);
				String ipAddress = String.format(expects[0][1], paramInt1, paramInt2, paramInt3, paramInt4, paramInt5);
				totalByString += System.nanoTime() - begin;
				list.add(sugoi);
				list.add(ipAddress);
			}

			// MessageFormat#format(String, Object...)
			{
				long begin = System.nanoTime();
				String sugoi = MessageFormat.format(expects[1][0], paramStr1, paramStr2);
				String ipAddress = MessageFormat.format(expects[1][1], paramInt1, paramInt2, paramInt3, paramInt4, paramInt5);
				totalByMessage += System.nanoTime() - begin;
				list.add(sugoi);
				list.add(ipAddress);
			}
		}
		System.out.println(String.format("String concatenation average = %d[ns]", totalByConcat / LOOP));
		System.out.println(String.format("String#format(String, Object...) average = %d[ns]", totalByString / LOOP));
		System.out.println(String.format("MessageFormat#format(String, Object...) average = %d[ns]", totalByMessage / LOOP));
	}

	/**
	 *A bench that stores formatted strings<br>
	 * {@link String#format(String, Object...)} vs {@link Formatter#format(String, Object...)}
	 */
	private void benchmark2() {
		final String[] expects = { "great!%s is%I'm a friend who is good at s!", "IP address%d.%d.%d.%d/%d" };

		long totalByString = 0;
		long totalByFormatter = 0;
		for (int i = 0; i < LOOP; i++) {
			String paramStr1 = randomString();
			String paramStr2 = randomString();
			int paramInt1 = random.nextInt(255);
			int paramInt2 = random.nextInt(255);
			int paramInt3 = random.nextInt(255);
			int paramInt4 = random.nextInt(255);
			int paramInt5 = random.nextInt(32);

			// StringBuilder + String#format
			{
				long begin = System.nanoTime();
				StringBuilder sb = new StringBuilder();
				sb.append(String.format(expects[0], paramStr1, paramStr2));
				sb.append(String.format(expects[1], paramInt1, paramInt2, paramInt3, paramInt4, paramInt5));
				totalByString += System.nanoTime() - begin;
			}

			// Formatter#format
			{
				long begin = System.nanoTime();
				try (Formatter formatter = new Formatter();) {
					formatter.format(expects[0], paramStr1, paramStr2);
					formatter.format(expects[1], paramInt1, paramInt2, paramInt3, paramInt4, paramInt5);
				}
				totalByFormatter += System.nanoTime() - begin;
			}
		}

		System.out.println(String.format("String#format(String, Object...) average = %d[ns]", totalByString / LOOP));
		System.out.println(String.format("Formatter#format(String, Object...) average = %d[ns]", totalByFormatter / LOOP));
	}
}

Output (average per million times)

===== benchmark1 =====
String concatenation average = 269[ns]
String#format(String, Object...) average = 2383[ns]
MessageFormat#format(String, Object...) average = 4572[ns]

===== benchmark2 =====
String#format(String, Object...) average = 2302[ns]
Formatter#format(String, Object...) average = 2101[ns]

result

benchmark1 --String concatenation is nearly 10 times faster than __String # format (String, Object ...) __ --However, __String # format (String, Object ...) __ is about 40 to 50% faster than __MessageFormat # format (String, Object ...) __.

benchmark2 --If you want to accumulate strings after replacing format strings, there is java.util.Formatter, which is slightly faster than combining StringBuilder and __String # format (String, Object ...) __.

Recommended Posts

Is String # format (String, Object ...) really slow?
Array is object
What is object orientation?
Output table from Map <String, Object> format with Thymeleaf3
Library to check if it is an IPv4 format string
Maybe this is object oriented
Standard API for string format
Explanation about Ruby String object