Performance between String.format and StringBuilder

What is “better” solely depends on your requirements:

  • For instance String Builder will be faster, but the code will be much more unreadable, and and it would be easier to make a mistake.

  • On the other hand String.format() produces more readable code at the cost of performance.

JMH benchmark to illustrate the performance difference (notice that the string builder code is longer and very hard to understand how the resulting string would look like):

@Fork(1)
@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Measurement(iterations = 10)
@Warmup(iterations = 10)
@BenchmarkMode(Mode.Throughput)
public class StringFormatBenchmark {
    private String name = "UserName";
    private String lName = "LUserName";
    private String nick = "UserNick";

    @Benchmark
    public void stringFormat(Blackhole blackhole) {
        final String result = String.format("Contact {name=%s, lastName=%s, nickName=%s}", name, lName, nick);
        blackhole.consume(result);
    }

    @Benchmark
    public void stringBuilder(Blackhole blackhole) {
        final StringBuffer sb = new StringBuffer("Contact {");
        sb.append(", name="").append(name)
                .append(", lastName="").append(lName)
                .append(", nickName="").append(nick)
                .append("}');
        final String result = sb.toString();
        blackhole.consume(result);
    }
}

And the results:

Benchmark                             Mode  Cnt      Score     Error   Units
StringFormatBenchmark.stringBuilder  thrpt   10  10617.210 ± 157.302  ops/ms
StringFormatBenchmark.stringFormat   thrpt   10    960.658 ±   7.398  ops/ms

For non performance critical code I prefer using the String.format(), because it’s easier and more pleasant to use. Also it’s visible what the resulting string would look like, by simply looking at the pattern. If I’m doing a performance critical code, or something that has to have a low GC impact, I would use a StringBuilder because it’s faster and can be reused.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)