0

In one of my java tools, I have some strings being created repeatedly with changing variables contained within like so:

for (String value : values) {
    // unrelated code omitted for clarity
    String outputValue = "blah blah blah " + foo + " blah " + bar + " blah";
    // code that writes out the created outputValue
}

I cant help but feel this isn't the best method for having "parameters" in a sense in the created String, as the non-parameter text is always the same. However, as I understand it, using String.format or similar to plug in these parameters would actually be less efficient than simple concatenation.

Is there a better/more efficient (with respect to time more than memory) way of creating these parametrised strings?

Quetzalcoatl
  • 3,037
  • 2
  • 18
  • 27
  • I don't know about more efficient during run time, but String.format() is more efficient in terms of typing. – Code-Apprentice Apr 07 '13 at 00:38
  • Subjectively, but then the link in Jiri Kremser's answer highlights that + concatenation time performance comes in at a ratio against `String.format` of around 5 to 6 respectively. – Quetzalcoatl Apr 07 '13 at 00:43

4 Answers4

0

as I understand it, using String.format or similar to plug in these parameters would actually be less efficient than simple concatenation.

What evidence do you have to support this? And why do you think that it would be worth worrying about in light of everything else your app is doing? Sounds like a micro-optimization to me.

I would recommend any templating solution, like Velocity, if your needs are extensive.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • This is in a hadoop reducer, and there are potentially millions of strings being passed through, so I thought anything I can do to improve it couldn't hurt. With regards to String.format, surely it has to process the string to identify where to add in these parameters, as opposed to just straight concatenation, which would surely be a cheaper process. – Quetzalcoatl Apr 07 '13 at 00:32
  • Still no evidence that I can see, besides "I say so." – duffymo Apr 07 '13 at 00:45
  • http://stackoverflow.com/questions/513600/should-i-use-javas-string-format-if-performance-is-important Comes in at a time ratio of 5:6 according to this. – Quetzalcoatl Apr 07 '13 at 00:45
  • I think the comments in that link make my point. Benchmarks like this aren't always meaningful. – duffymo Apr 07 '13 at 00:46
  • Perhaps, I'll put it to the test in my scenario anyhow. – Quetzalcoatl Apr 07 '13 at 00:48
  • That's a better idea. Much more definitive than asking here. – duffymo Apr 07 '13 at 00:49
  • My asking here was to see if there's a known better method than pure + concatenation that I could try, all I'm seeking are more ideas. – Quetzalcoatl Apr 07 '13 at 00:50
0

Most efficient way would probably be with a StringBuilder.

StringBuilder builder = new StringBuilder("blah blah blah");
builder.append(yourvalue).append("blah");

What StringBuilder allows for is the avoidance of multiple and expensive String concatenations. When you're finally done, you just call builder.toString() to get the final String.

BlackBox
  • 2,223
  • 1
  • 21
  • 37
  • This wouldn't really achieve anything other than arguably "cleaner" looking code - bear in mind im not appending to the same string per value, so I'd need a new builder each time. – Quetzalcoatl Apr 07 '13 at 00:34
  • 2
    The compiler would do this anyway. – Boris the Spider Apr 07 '13 at 00:34
  • Its not "cleaner" code, its more efficient code. And no, the compiler would not do this. – BlackBox Apr 07 '13 at 00:35
  • Yes it does, under the hood the string concatenation is handled by StringBuilders, and how "clean" it looks is subjective. – Quetzalcoatl Apr 07 '13 at 00:36
  • From what I've read, the compiler "might" do it. It is not always the case, unless you have resources I have not seen? Would be glad to learn more. – BlackBox Apr 07 '13 at 00:40
0

It is trade off between nice to read one-liner and super efficient hard to read piece of code using StringBuffer or StringBuilder. So there is no universal answer to this question, I think. Depends on what quality is more valuable for you (readability -> maintainability or performance), how many times it is called, is it really a bottleneck in your app, etc.

Here is nice summarization

Community
  • 1
  • 1
Jiri Kremser
  • 12,471
  • 7
  • 45
  • 72
  • The compiler will use a `StringBuilder` for this as it is a single line. So it's a toss up between formatting and `+`. – Boris the Spider Apr 07 '13 at 00:36
  • Right, for this particular case + is equivalent to `StringBuilder`, but if there was `outputValue += "a" + foo + "b" + bar + "c"`, it wouldn't. I like to mention things explicitly and do not rely on some magic under the hood :) – Jiri Kremser Apr 07 '13 at 00:40
-1

Don't attempt to optimize this beyond using String.format or StringBuilder.append instead of String concatenation with + (especially in loops).

pmorken
  • 764
  • 4
  • 11
  • StringBuilders are not the solution here - there is no iterative appending to the same string going on here. It's a new string each time, and concatenation is being handled by StringBuilders under the hood anyway. – Quetzalcoatl Apr 07 '13 at 00:37
  • This is a single line, the compiler will use a `StringBuilder`. As the OP points out, `String.format` is slower than `+` in this case (as the compiler will substitute a `StringBuilder`). – Boris the Spider Apr 07 '13 at 00:38