Search code examples
javastringbuilderstring.format

Performance between String.format and StringBuilder


To concatenate String we often use StringBuilder instead of String + String, but also we can do the same with String.format which returns the formatted string by given locale, format and arguments.

Examples:

Concatenate the string with StringBuilder

String concatenateStringWithStringBuilder(String name, String lName, String nick) {
    final StringBuilder sb = new StringBuilder("Contact {");
    sb.append(", name='").append(name)
      .append(", lastName='").append(lName)
      .append(", nickName='").append(nick)
      .append('}');
    return sb.toString();
}

Concatenate the string with StringFormat:

String concatenateStringWithStringFormat(String name, String lName, String nick) {
    return String.format("Contact {name=%s, lastName=%s, nickName=%s}", name, lName, nick);
}

In performance, is String.Format as efficient as StringBuilder? Which one is better to concatenate strings and why?

UPDATE

I checked the similar question, but doesn´t answer my question. So far I have used StringBuilder to concatenate strings, should I follow it using? Or should I use String.format? the question is which one is better and why?


Solution

  • After doing a little test with StringBuilder vs String.format I understood how much time it takes each of them to solve the concatenation. Here the snippet code and the results

    Code:

    String name = "stackover";
    String lName = " flow";
    String nick = " stackoverflow";
    String email = "stackoverflow@email.com";
    int phone = 123123123;
    
    //for (int i = 0; i < 10; i++) {
    long initialTime1 = System.currentTimeMillis();
    String response = String.format(" - Contact {name=%s, lastName=%s, nickName=%s, email=%s, phone=%d}",
                                    name, lName, nick, email, phone);
    long finalTime1 = System.currentTimeMillis();
    long totalTime1 = finalTime1 - initialTime1;
    System.out.println(totalTime1 + response);
    
    long initialTime2 = System.currentTimeMillis();
    final StringBuilder sb = new StringBuilder(" - Contact {");
    sb.append("name=").append(name)
      .append(", lastName=").append(lName)
      .append(", nickName=").append(nick)
      .append(", email=").append(email)
      .append(", phone=").append(phone)
      .append('}');
    String response2 = sb.toString();
    long finalTime2 = System.currentTimeMillis();
    long totalTime2 = finalTime2 - initialTime2;
    System.out.println(totalTime2 + response2);
    //}
    

    After of run the code several times, I saw that String.format takes more time:

    String.format: 46: Contact {name=stackover, lastName= flow, nickName= stackoverflow, email=stackoverflow@email.com, phone=123123123}
    StringBuilder: 0: Contact {name=stackover, lastName= flow, nickName= stackoverflow, email=stackoverflow@email.com, phone=123123123}
    String.format: 38: Contact {name=stackover, lastName= flow, nickName= stackoverflow, email=stackoverflow@email.com, phone=123123123}
    StringBuilder: 0: Contact {name=stackover, lastName= flow, nickName= stackoverflow, email=stackoverflow@email.com, phone=123123123}
    String.format: 51: Contact {name=stackover, lastName= flow, nickName= stackoverflow, email=stackoverflow@email.com, phone=123123123}
    StringBuilder: 0: Contact {name=stackover, lastName= flow, nickName= stackoverflow, email=stackoverflow@email.com, phone=123123123}
    

    But if I run the same code inside a loop, the result change.

    String.format: 43: Contact {name=stackover, lastName= flow, nickName= stackoverflow, email=stackoverflow@email.com, phone=123123123}
    StringBuilder: 0: Contact {name=stackover, lastName= flow, nickName= stackoverflow, email=stackoverflow@email.com, phone=123123123}
    String.format: 1: Contact {name=stackover, lastName= flow, nickName= stackoverflow, email=stackoverflow@email.com, phone=123123123}
    StringBuilder: 0: Contact {name=stackover, lastName= flow, nickName= stackoverflow, email=stackoverflow@email.com, phone=123123123}
    String.format: 1: Contact {name=stackover, lastName= flow, nickName= stackoverflow, email=stackoverflow@email.com, phone=123123123}
    StringBuilder: 0: Contact {name=stackover, lastName= flow, nickName= stackoverflow, email=stackoverflow@email.com, phone=123123123}
    

    The first time String.format runs it takes more time, after of that the time is shorter even though it does not become constant as a result of StringBuilder

    As @G.Fiedler said: "String.format has to parse the format string..."

    With these results it can be said that StringBuilder is more efficient thanString.format