WEB4J - Minimalist Java Web Application Framework

String concatenation does not scale

To build Strings dynamically, one may use either the String concatenation operator + or the StringBuilder class. (If JDK 1.5 is available, StringBuilder is generally preferred over StringBuffer, since it avoids internal synchronization costs.) In the great majority of cases, only a few items are concatenated, and either style may be used freely, according to taste, without concern for performance.

On relatively rare occasions, however, when performing extensive String manipulation, replacing + with StringBuilder.append is likely recommended. Compilers often implement concatenation operations by creating  intermediate objects in the background. Such intermediate objects are not needed by StringBuilder.

Cases in which + is very likely acceptable :

Here is an illustration of the difference in execution speed between + and StringBuilder.append, when performing many concatenations. Clearly, even the + operator is quite fast, and the difference is not going to be noticeable in the great majority of cases encountered in practice.

Example runs (class appears below) :

(Here, -Xint turns off the HotSpot compiler. This ensures the program is always interpreted, and will execute in a uniform environment from start to finish, without any compilation into native code.)

>java -cp . -Xint AvoidConcatenation 10
Num iterations: 10
Task using + operator: 0 milliseconds
Task using StringBuilder.append: 0 millisecond

>java -cp . -Xint AvoidConcatenation 100
Num iterations: 100
Task using + operator: 10 milliseconds
Task using StringBuilder.append: 0 milliseconds

>java -cp . -Xint AvoidConcatenation 1000
Num iterations: 1000
Task using + operator: 210 milliseconds
Task using StringBuilder.append: 0 milliseconds

>java -cp . -Xint AvoidConcatenation 10000
Num iterations: 10000
Task using + operator: 36913 milliseconds
Task using StringBuilder.append: 10 milliseconds



/**
* Illustrates the speed difference between + operator and
* StringBuilder.append, when performing many concatenations.
*/
public final class AvoidConcatenation {

  /**
  * Takes a single argument : the number of iterations to perform.
  */
  public static void main (String... arguments) {
    fNumIterations = new Integer(arguments[0]);

    long start = System.currentTimeMillis();
    doWithConcatenationOperator();
    long finish = System.currentTimeMillis();

    System.out.println("Num iterations: " + fNumIterations);
    StringBuilder message = new StringBuilder();
    message.append("Task using + operator: ");
    message.append( finish - start );
    message.append(" milliseconds");
    System.out.println(message);

    start = System.currentTimeMillis();
    doWithStringBuilder();
    finish = System.currentTimeMillis();

    message = new StringBuilder("Task using StringBuilder.append: ");
    message.append( finish - start );
    message.append(" milliseconds");
    System.out.println(message);
  }

  // PRIVATE //
  private static Integer fNumIterations;

  private static String doWithConcatenationOperator() {
    String result = "start";
    for (int idx = 0; idx < fNumIterations; idx++) {
      result = result + "blah";
    }
    return result;
  }

  private static String doWithStringBuilder() {
    StringBuilder result = new StringBuilder("start");
    for (int idx = 0; idx < fNumIterations; idx++) {
      result.append("blah");
    }
    return result.toString();
  }
}
 



See Also :
Implementing toString
Time execution speed
Would you use this technique?
Yes   No   Undecided   
Add your comment to this Topic :

© 2008 Hirondelle Systems | Source Code | Contact | License | Quotes | RSS
Individual code snippets can be used under this license - Last updated on September 6, 2008.
Over 98,000 visits last month - Built with WEB4J.
- In Memoriam : Bill Dirani -