Combining strings in Java
At work we do lots of code reviews. This, of course, is a good thing. I saw a couple of times (including on my own code) a statement that looks kinda like this:
System.out.println("The foo took " + time + "ms");
The code review comment was something like "Use String.format for this"So I did.Then today I got to thinking. Why? Certainly it's the orthodoxy. Hell, it's the interview question of why it's bad to add strings together -- because it works the garbage collector hard with all the temporary objects that are created.But something felt odd. A bit of Googling seemed to get to the same conclusion... but I wanted to to do it myself -- so I wrote some code.
public class speedtest {public static void main(String[] args) {for (int i = 0; i < 5; i++) {runSample(false);}System.out.println("Time\tMem");for (int i = 0; i < 100; i++) {runSample(true);}}private static void runSample(Boolean shouldPrint) {long startTime = System.nanoTime();long startMem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();for (int i = 0; i < 10000; i++) {// String s = String.format("This is the %d run of this program %d some more stuff and %d more %d", i, i, i, i); String s = "This is the " + i + " run of this program " + i + " some more stuff and " + i + " more " + i;}long endTime = System.nanoTime();long endMem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();long time = endTime - startTime;long mem = endMem - startMem;if (shouldPrint) {System.out.format("%f\t%d\n", time / 1000000.0, mem);}System.gc();System.gc();System.gc();System.gc();System.gc();}}
The line in red is the line under test, green is the other one that I've tested. So, what I'm doing is combining four strings and four ints. In either case the end result is the same.The same except for the time and memory consumed.
String.Format | + operator | Difference | |
Average time (ms) | 20.062 | 1.680 | 8.37% |
Std dev time (ms) | 1.418 | 0.513 | 36.15% |
Average memory | 12,923,137 | 6,638,326 | 51.37% |
Std dev memory | 44,314 | 77,869 | 175.72% |
The upshot is that String.format takes over 11 times longer to run while using twice the memory.I had a hunch that it would be faster, but that it would be that much faster is rather shocking -- and that it would at the same time use that much less memory at the same time.Just a data point...Of course, this isn't a substitute String.format -- or even more properly StringBuffer. Formatting has a place when you're actually formatting something or if you need to localize the thing you're outputting.I'm still rather surprised though.