19

is there any difference in performance between these two idioms ?

String firstStr = "Hello ";
String secStr   = "world";
String third = firstStr +  secStr;

and

String firstStr = "Hello ";
String secStr   = "world";
String third = String.format("%s%s",firstStr , secStr);

I know that concatenation with + operator is bad for performance specially if the operation is done a lot of times, but what about String.format() ? is it the same or it can help to improve performance?

Leo
  • 1,829
  • 4
  • 27
  • 51
  • Concatenating constant strings, the first example will be clearly optimized by the compiler because the resulting strings is a constant and shouldn't influence the performance at all. – Smutje Feb 16 '14 at 00:21
  • 2
    @Smutje The third string is not a constant. It would be if the first two were `final`. – Sotirios Delimanolis Feb 16 '14 at 00:23
  • Right, I am so used to having local variables as final by default/if possible, I nearly forgot that it's not mandatory. :-) – Smutje Feb 16 '14 at 00:24
  • 1
    They are two different animals. Concatenation is faster (and the compiler is smart enough to optimize things on their own most of the time, you only need to resort to `StringBuilder` inside loops and such). Formatting is slower, but will do a lot more (for instance, you can justify, print tabulated data by setting widths properly, etc). – Anthony Accioly Feb 16 '14 at 00:27

3 Answers3

19

The second one will be even slower (if you look at the source code of String.format() you will see why). It is just because String.format() executes much more code than the simple concatenation. And at the end of the day, both code versions create 3 instances of String. There are other reasons, not performance related, to use String.format(), as others already pointed out.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
8

First of all, let me just put a disclaimer against premature optimization. Unless you are reasonably sure this is going to be a hotspot in your program, just choose the construct that fits your program the best.

If you are reasonably sure, however, and want to have good control over the concatenation, just use a StringBuilder directly. That's what the built-in concatenation operation does anyway, and there's no reason to assume that it's slow. As long as you keep the same StringBuilder and keep appending to it, rather than risking creating several in a row (which would have to be "initialized" with the previously created data), you'll have proper O(n) performance. Especially so if you make sure to initialize the StringBuilder with proper capacity.

That also said, however, a StringBuilder is, as mentioned, what the built-in concatenation operation uses anyway, so if you just keep all your concatenations "inline" -- that is, use A + B + C + D, rather than something like e = A + B followed by f = C + D followed by e + f (this way, the same StringBuilder is used and appended to throughout the entire operation) -- then there's no reason to assume it would be slow.

EDIT: In reply to your comment, I'd say that String.format is always slower. Even if it appends optimally, it can't do it any faster than StringBuilder (and therefore also the concatenation operation) does anyway, but it also has to create a Formatter object, do parsing of the input string, and so on. So there's more to it, but it still can't do the basic operation any faster.

Also, if you look internally in how the Formatter works, you'll find that it also (by default) uses a StringBuilder, just as the concatenation operation does. Therefore, it does the exact same basic operation -- that is, feeding a StringBuilder with the strings you give it. It just does it in a far more roundabout way.

Dolda2000
  • 25,216
  • 4
  • 51
  • 92
  • thanks, you're right, StringBuilder is the best option when buildng a String, although my question was more a comparison between the two idioms listed in the description. – Leo Feb 16 '14 at 00:38
1

As described in this excellent answer , you would rather use String.format, but mainly because of localization concerns.

Suppose that you had to supply different text for different languages, in that case then using String.format - you can just plug in the new languages(using resource files). But concatenation leaves messy code.

See : Is it better practice to use String.format over string Concatenation in Java?

Community
  • 1
  • 1
Caffeinated
  • 11,982
  • 40
  • 122
  • 216