Java: flushing StringBuffer content

Everything,

I was wondering if it makes sense to clear the contents of StringBuffer with setLength (0). that is, it is better to do:

while (<some condition>) { stringBufferVariable = new StringBuffer(128); stringBufferVariable.append(<something>) .append(<more>) ... ; Append stringBufferVariable.toString() to a file; stringBufferVariable.setLength(0); } 

My questions:
1> Will it have better performance than having a String object to add content to?

I'm not sure how reinitializing the StringBuffer variable will affect performance and therefore the question.

Please write your comments

[edit]: I removed the second question about comparing with StringBuilder, since I realized that nothing more needs to be found based on the answers.

+4
source share
6 answers

Better than string concatenation?

If you ask

 stringBufferVariable.append("something") .append("more"); ... 

will work better than concatenation with + , then yes, usually. That all of these classes exist. Creating an object is expensive compared to updating the values ​​in a char array.

It seems that if not all compilers now convert string concatenation to using StringBuilder in simple cases, such as str = "something" + "more" + "..."; . The only performance difference I can see is that the compiler will not have the advantage of setting the initial size. Tests will tell you if the difference is enough. Using + will do for more readable code.

From what I read, the compiler apparently cannot optimize the concatenation performed in the loop when it is something like

 String str = ""; for (int i = 0; i < 10000; i++) { str = str + i + ","; } 

so in these cases, you still want to explicitly use StringBuilder.

StringBuilder vs StringBuffer

StringBuilder not thread safe, and StringBuffer is a StringBuffer , but otherwise they do not match. The synchronization performed by StringBuffer makes it slower, so StringBuilder is faster and should be used if you do not need synchronization.

Should I use setLength ?

What your example looks like now. I don't think the setLength call gets anything as you create a new StringBuffer for each pass through the loop. What you really have to do is

 StringBuilder sb = new StringBuilder(128); while (<some condition>) { sb.append(<something>) .append(<more>) ... ; // Append stringBufferVariable.toString() to a file; sb.setLength(0); } 

This avoids unnecessary object creation, and setLength will update the int variable in this case.

+7
source

I just focus on this part of the question. (Other parts were asked and answered SO many times.)

I was wondering if it makes sense to clear the contents of StringBuffer with setLength (0).

It depends on the Java class classes used. In some older versions of Java, Sun StringBuffer.toString () was implemented under the assumption that the sb.toString() call is the last thing that is done with the buffer. The original underlying array of StringBuffer becomes part of the string returned by toString() . A subsequent attempt to use StringBuffer led to the creation of a new support and initialization array by copying the contents of the String. Thus, reusing StringBuffer as your code tries to do will actually make the application slower.

With Java 1.5 and later, this is best done:

  bufferedWriter.append(stringBufferVariable); stringBufferVariable.setLength(0); 

This should copy the characters directly from StringBuilder to the file buffer without the need to create a temporary line. Assuming the StringBuffer declaration goes beyond the loop, setLength (0) then allows the buffer to be reused.

Finally, you should only worry about all this if you have evidence that this piece of code (or is likely to be) is a bottleneck. "Premature optimization is the root of all evil" blah, blah.

+3
source

In question 2, StringBuilder will work better than StringBuffer. StringBuffer is thread safe, i.e. methods are synchronized. String Builder is not synchronized. Therefore, if the code that you have is run by the ONE thread, then StringBuilder will have better performance since it does not have the overhead of performing synchronization.

As a hint, please see the API for StringBuffer and StringBuilder for more information.

You may also be interested in this article: Sad tragedy in the micro-optimization test

+1
source

1> Will it have better performance than having a String object to add content to?

Yes, string concatenation is slow as you keep creating new String objects.

2> If using StringBuilder will work better than StringBuffer, then why?

Have you read the API description for StringBuilder and / or StringBuffer? It is issued there.

I'm not sure how reinitializing the StringBuffer variable will affect performance and therefore the question.

Well create a test program. Create a test that creates a new StringBuffer / Builder each time. Then run the test and just reset the characters to 0, and then compare the time.

0
source

Maybe I don’t understand something in your question ... why do you set the length to 0 at the bottom if you just create a new one at the beginning of each iteration?

Assuming the variable is a local variable for the method or that it will not be used by multiple threads if it is declared outside the method (if it is outside the method, your code probably has problems, though), then do this StringBuilder.

If you declare a StringBuilder outside the loop, you do not need to create a new one each time you enter the loop, but you want to set the length at the end of the loop to 0.

If you declare a StringBuilder inside a loop, you do not need to specify a length of up to 0 at the end of the loop.

It is likely that declaring it outside the loop and setting the length to 0 will be faster, but I would measure both, and if there isn’t much difference, declare a variable inside the loop. Good practice is to limit the scope of variables.

0
source

Yes! setLength(0) is a great idea! what is this. something faster would be to drop the stringBuffer and make a new one. faster, cannot say anything about memory efficiency :)

-1
source

All Articles