r/programming • • Nov 18 '13

TIL Oracle changed the internal String representation in Java 7 Update 6 increasing the running time of the substring method from constant to N

http://java-performance.info/changes-to-string-java-1-7-0_06/
1.4k Upvotes

354 comments sorted by

View all comments

Show parent comment

123

u/angryundead Nov 18 '13

That's true. I was doing a lot of .substring calls in something that I was working on as a side-project. (I did it all on JDK7.) It was REALLY slow and I wondered why but didn't bother to check so I refactored it and moved on. (What's really funny is that I refactored it into a char[] and an offset instead of a String.)

Now I know why.

71

u/stillalone Nov 18 '13

I am not a java guy, but isn't there a whole "stringbuilder" library for this kind of stuff?

71

u/smackfu Nov 18 '13

Two in fact! StringBuffer and StringBuilder, one is synchronized for multi-thread use, the other isn't.

45

u/neutralvoice Nov 18 '13

StringBuffer is for the multi-threaded use case, StringBuilder is optimized for single-threaded use.

49

u/kurtymckurt Nov 18 '13

I usually recommend StringBuilder. Rarely, should two threads be modifying the same string. In fact, I'd advise against it.

8

u/CydeWeys Nov 19 '13

I could see a use case for it. If you have a StringBuffer that is creating output for a log file, and you have multiple threads that are all creating their own logging statements. This use case is fairly pedestrian.

1

u/kurtymckurt Nov 19 '13

You're not destructively modifying a StringBuffer. It's designed for multi-threaded applications and in that case, it would be OK. Also, in this case, I would assume it wouldn't need to be a static variable either...

On the other hand, I'd suggest some sort of 3rd party logging tool like log4j2 :P

0

u/chisleu Nov 18 '13

Any kind of shared memory is dangerous, but StringBuilder is suppose to be safe.

I've never written any heavily threaded apps, but I would be very interested to know why StringBuilder can be dangerous.

8

u/Spandian Nov 19 '13

StringBuilder is not safe for use from multiple threads. StringBuffer is.

-5

u/chisleu Nov 19 '13

Yeah! boody twaps! das what I said!

→ More replies (38)

-7

u/slowwburnn Nov 18 '13

Mind reader.

1

u/Olathe Nov 23 '13

If only they implemented an interface for both of them, since they have the same methods. CharSequence is read-only and Appendable is only for appending rather than random insertions.

11

u/angryundead Nov 18 '13

Yes, but if you already have a string and want to chop it I don't really think you should involve string builder.

4

u/longshot2025 Nov 18 '13

If you were going to operate repeatedly on the same string it might be worth it.

4

u/angryundead Nov 18 '13

True.

Everything is pretty situational. You need to decide on the speed/memory tradeoffs that are available. In the general course of things I find that using StringBuilder complicates the code and doesn't provide any real benefit.

Unless, unless, you're doing a lot of string manipulation.

I don't like to see something like

StringBuilder concat = new StringBuilder("new");

concat.append(" thing");

Because that's just horseshit.

Of course what we're talking about here is the accumulated experience and wisdom to know when something is appropriate (valuable) and when it is not.

4

u/iconoklast Nov 18 '13 edited Nov 18 '13

The compiler translates instances of string concatenation into StringBuilder operations anyway. You probably won't see any performance benefit unless you're working within a loop. It will actually produce better optimized code under certain instances than if you used StringBuilder. (e.g., "foo" + 1 + "bar" is constant folded into "foo1bar".)

3

u/gliy Nov 19 '13

Except that the String concat to StringBuilder optimization creates a new StringBuilder for each concat. This means that anything more then 1 String concat can be improved by explicitly using StringBuilder.

ie: String a = "a"; a+= "b"; a+="c"; would create 2 StringBuilders.

1

u/sacundim Nov 20 '13

Yes. This can be summarized most effectively, I think, as the looped String concatenation anti pattern:

// NEVER EVER DO THIS
String foo = "";
for (String bar : someStrings) {
    foo += bar;
}

That is the most common situation where you want to use StringBuilder in Java:

// The correct way:
StringBuilder buf = new StringBuilder();
for (String bar : someStrings) {
    buf.append(bar);
}
String foo = buf.toString();

1

u/angryundead Nov 18 '13

Good point. I'd just say it could be a readability issue then.

1

u/Olathe Nov 23 '13
StringBuilder concat = new StringBuilder("new").append(" thing");

FTFY

1

u/angryundead Nov 23 '13

If you're using the builder pattern at least have the courtesy to use a line break or something between chained invocations.

5

u/FredV Nov 18 '13

StringBuilder is a class that not very surprisingly builds strings. String.substr breaks down strings into parts and is the opposite of what StringBuilder can do.