We have to build Strings all the time for log output and so on. Over the JDK versions we have learned when to use StringBuffer
(many appends, thread safe) and StringBuilder
(many appends, non-thread-safe).
What's the advice on using String.format()
? Is it efficient, or are we forced to stick with concatenation for one-liners where performance is important?
e.g. ugly old style,
String s = "What do you get if you multiply " + varSix + " by " + varNine + "?";
vs. tidy new style (String.format, which is possibly slower),
String s = String.format("What do you get if you multiply %d by %d?", varSix, varNine);
Note: my specific use case is the hundreds of 'one-liner' log strings throughout my code. They don't involve a loop, so StringBuilder
is too heavyweight. I'm interested in String.format()
specifically.
I took hhafez code and added a memory test:
private static void test() {
Runtime runtime = Runtime.getRuntime();
long memory;
...
memory = runtime.freeMemory();
// for loop code
memory = memory-runtime.freeMemory();
I run this separately for each approach, the '+' operator, String.format and StringBuilder (calling toString()), so the memory used will not be affected by other approaches. I added more concatenations, making the string as "Blah" + i + "Blah"+ i +"Blah" + i + "Blah".
The result are as follow (average of 5 runs each):
Approach Time(ms) Memory allocated (long)
'+' operator 747 320,504
String.format 16484 373,312
StringBuilder 769 57,344
We can see that String '+' and StringBuilder are practically identical time-wise, but StringBuilder is much more efficient in memory use. This is very important when we have many log calls (or any other statements involving strings) in a time interval short enough so the Garbage Collector won't get to clean the many string instances resulting of the '+' operator.
And a note, BTW, don't forget to check the logging level before constructing the message.
Conclusions: