Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to reuse a StringBuilder in a loop?

I've a performance related question regarding use of StringBuilder. In a very long loop I'm manipulating a StringBuilder and passing it to another method like this:

for (loop condition) {     StringBuilder sb = new StringBuilder();     sb.append("some string");     . . .     sb.append(anotherString);     . . .     passToMethod(sb.toString()); } 

Is instantiating StringBuilder at every loop cycle is a good solution? And is calling a delete instead better, like the following?

StringBuilder sb = new StringBuilder(); for (loop condition) {     sb.delete(0, sb.length);     sb.append("some string");     . . .     sb.append(anotherString);     . . .     passToMethod(sb.toString()); } 
like image 474
Pier Luigi Avatar asked Oct 28 '08 07:10

Pier Luigi


People also ask

Can I reuse StringBuilder?

So, yes, as it seems reusing is indeed faster, at least if you test it in a tight loop with a small memory footprint.

How do you clean StringBuilder?

A simple solution to clear a StringBuilder / StringBuffer in Java is calling the setLength(0) method on its instance, which causes its length to change to 0. The setLength() method fills the array used for character storage with zeros and sets the count of characters used to the given length.

How do I iterate through a StringBuilder?

A for-loop can iterate over the characters in a StringBuilder. We access the length() method to get the StringBuilder's size and then use charAt() to access chars. Length This method returns the count of characters in the StringBuilder.

Which is more time efficient StringBuilder or StringBuffer?

StringBuilder is faster than StringBuffer because it's not synchronized .


1 Answers

The second one is about 25% faster in my mini-benchmark.

public class ScratchPad {      static String a;      public static void main( String[] args ) throws Exception {         long time = System.currentTimeMillis();         for( int i = 0; i < 10000000; i++ ) {             StringBuilder sb = new StringBuilder();             sb.append( "someString" );             sb.append( "someString2"+i );             sb.append( "someStrin4g"+i );             sb.append( "someStr5ing"+i );             sb.append( "someSt7ring"+i );             a = sb.toString();         }         System.out.println( System.currentTimeMillis()-time );         time = System.currentTimeMillis();         StringBuilder sb = new StringBuilder();         for( int i = 0; i < 10000000; i++ ) {             sb.delete( 0, sb.length() );             sb.append( "someString" );             sb.append( "someString2"+i );             sb.append( "someStrin4g"+i );             sb.append( "someStr5ing"+i );             sb.append( "someSt7ring"+i );             a = sb.toString();         }         System.out.println( System.currentTimeMillis()-time );     } } 

Results:

25265 17969 

Note that this is with JRE 1.6.0_07.


Based on Jon Skeet's ideas in the edit, here's version 2. Same results though.

public class ScratchPad {      static String a;      public static void main( String[] args ) throws Exception {         long time = System.currentTimeMillis();         StringBuilder sb = new StringBuilder();         for( int i = 0; i < 10000000; i++ ) {             sb.delete( 0, sb.length() );             sb.append( "someString" );             sb.append( "someString2" );             sb.append( "someStrin4g" );             sb.append( "someStr5ing" );             sb.append( "someSt7ring" );             a = sb.toString();         }         System.out.println( System.currentTimeMillis()-time );         time = System.currentTimeMillis();         for( int i = 0; i < 10000000; i++ ) {             StringBuilder sb2 = new StringBuilder();             sb2.append( "someString" );             sb2.append( "someString2" );             sb2.append( "someStrin4g" );             sb2.append( "someStr5ing" );             sb2.append( "someSt7ring" );             a = sb2.toString();         }         System.out.println( System.currentTimeMillis()-time );     } } 

Results:

5016 7516 
like image 181
Epaga Avatar answered Sep 22 '22 19:09

Epaga