Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use StringBuilder.append instead of += when building up a String. #39

Merged
merged 1 commit into from
Aug 1, 2011

Conversation

kevinsawicki
Copy link
Contributor

This reduces the number of total String objects created and keeps the same contents in a StringBuilder instance instead.

This reduces the number of total strings created.
@bwmcadams
Copy link
Contributor

Java's compiler automatically does this optimization for you since either 5 or 6, so not necessary.

@bwmcadams bwmcadams closed this Jul 29, 2011
@kevinsawicki
Copy link
Contributor Author

Then why do so many other places in the mongo-java-driver use StringBuilder.append instead of += on String objects? These were the only 3 classes that use +=.

@kevinsawicki
Copy link
Contributor Author

Try running both of these and see if you get the same amount of time to complete:

public static void main( String[] args ) {
    long start = System.currentTimeMillis();
    String a = "test";
    for ( int i = 0; i < 10000; i++ ) {
        a += Integer.valueOf( i ).toString();
    }
    System.out.println( System.currentTimeMillis() - start );
}

public static void main( String[] args ) {
    long start = System.currentTimeMillis();
    StringBuilder a = new StringBuilder( "test" );
    for ( int i = 0; i < 10000; i++ ) {
        a.append( Integer.valueOf( i ).toString() );
    }
    System.out.println( System.currentTimeMillis() - start );
}

@mebigfatguy
Copy link
Contributor

The java compiler will replace += with StringBuilder, but kevin is right. His code only generates one new StringBuilder(), many append calls, and one toString call. Whereas, the a += ... generates 'n' new StringBuilder() calls, the same number of appends, and 'n' toString calls.

Creates alot of garbage

btw kevin, the toString() call on the Integer is superfluous when calling append.

@bwmcadams
Copy link
Contributor

I stand corrected. Ill reopen this shortly. Thanks for the clarifications as
well.
On Jul 29, 2011 8:33 PM, "mebigfatguy" <
[email protected]>
wrote:

The java compiler will replace += with StringBuilder, but kevin is right.
His code only generates one new StringBuilder(), many append calls, and one
toString call. Whereas, the a += ... generates 'n' new StringBuilder()
calls, the same number of toStrings, and 'n' toString calls.

Creates alot of garbage

Reply to this email directly or view it on GitHub:
#39 (comment)

@kevinsawicki
Copy link
Contributor Author

btw kevin, the toString() call on the Integer is superfluous when calling append.

Yes, I am aware of that, I was just trying to create a simple but explicit example of appending a String to a String.

Is this pull request going to be reopened?

@bwmcadams bwmcadams reopened this Aug 1, 2011
bwmcadams pushed a commit that referenced this pull request Aug 1, 2011
Use StringBuilder.append instead of += when building up a String.
@bwmcadams bwmcadams merged commit 54fdfe1 into mongodb:master Aug 1, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants