Concatenation operator versus BufferString

In this tip we are going to check one of the most important differences between using the concatenation operator and the string buffers. To be honest, many of us we are accustom to using the concatenation operator instead of buffer strings, mainly because it is much easier and clear to use the ‘+’ operator. It can be also because of the fact that when we learn to program this is one of the first things we learn. In many cases there aren’t many differences between one method and the other one. However, in some circumstances the difference between using one method or another can be huge, especially when we are programming heavy applications with large number of iterations in the loops.

Another class used for cancatenation is StringBuilder which was introduced in the 5.0 java version. The class is similar to StringBuilder but there are two main differences. One is the performance which is slightly better than StrinbBuffer and the other one is that this method is not synchronized.

The following code in Java shows us these differences in performance:

package test;

/**
 * The objective of this class is to show the difference in terms of performance between the
 * StringBuffer and the concatenation operator
 * 
 * @author ProgrammingWorkshop
 * @version 1.0
 */
public class Performance {

	abstract class Check {
		protected abstract void concat(int times);

		protected String TEST_STRING = "This is a test";

		public void checkTime(int times) {
			long initialTime = System.currentTimeMillis();
			concat(times);
			System.out.println("Number of operations: " + times
					+ ". Time in seconds for " + this.getClass().getSimpleName() + " is: "
					+ ((System.currentTimeMillis() - initialTime) / 1000f));

		}
	}

	class ConcatOperatorCheck extends Check {
		@SuppressWarnings("unused")
		public void concat(int times) {
			String value = "";
			for (int i = 0; i < times; i++) {
				value += TEST_STRING;
			}
		}
	}

	class StringBufferCheck extends Check {
		public void concat(int times) {
			StringBuffer value = new StringBuffer();
			for (int i = 0; i < times; i++) {
				value.append(TEST_STRING);
			}
		}
	}

	class StringBuilderCheck extends Check {
		public void concat(int times) {
			StringBuilder value = new StringBuilder();
			for (int i = 0; i < times; i++) {
				value.append(TEST_STRING);
			}
		}
	}

	public static void main(String args[]) {
		Performance p = new Performance();
		for (int i = 1; i <= 5; i++) {
			int times = i * 10000;
			p.new ConcatOperatorCheck().checkTime(times);
			p.new StringBufferCheck().checkTime(times);
			p.new StringBuilderCheck().checkTime(times);
			System.out.println("-----------------------------------------------------------");
		}
		p.new StringBufferCheck().checkTime(5000000);
		p.new StringBuilderCheck().checkTime(5000000);
	}
}

The application output for different numbers of iterations is this:

Number of operations: 10000. Time in seconds for ConcatOperatorCheck is: 0.737
Number of operations: 10000. Time in seconds for StringBufferCheck is: 0.002
Number of operations: 10000. Time in seconds for StringBuilderCheck is: 0.0
———————————————————–
Number of operations: 20000. Time in seconds for ConcatOperatorCheck is: 2.228
Number of operations: 20000. Time in seconds for StringBufferCheck is: 0.001
Number of operations: 20000. Time in seconds for StringBuilderCheck is: 0.002
———————————————————–
Number of operations: 30000. Time in seconds for ConcatOperatorCheck is: 4.822
Number of operations: 30000. Time in seconds for StringBufferCheck is: 0.001
Number of operations: 30000. Time in seconds for StringBuilderCheck is: 0.0
———————————————————–
Number of operations: 40000. Time in seconds for ConcatOperatorCheck is: 8.855
Number of operations: 40000. Time in seconds for StringBufferCheck is: 0.001
Number of operations: 40000. Time in seconds for StringBuilderCheck is: 0.001
———————————————————–
Number of operations: 50000. Time in seconds for ConcatOperatorCheck is: 14.429
Number of operations: 50000. Time in seconds for StringBufferCheck is: 0.001
Number of operations: 50000. Time in seconds for StringBuilderCheck is: 0.002
———————————————————–
Number of operations: 5000000. Time in seconds for StringBufferCheck is: 0.189
Number of operations: 5000000. Time in seconds for StringBuilderCheck is: 0.117

As we can see when we use the StringBuffer and StringBuilder objects the time we get is much lesser than when we use the concatenation operator.

There is another thing to point out. The performance when we use the concatenation operator is much worse when the number of operations increases. The mathematic progression is not lineal but exponential.

Therefore if you are going to use the concatenation operator make sure you are not going to use it in a loop with many concatenation operations.

In normal situations where we are not worry about multiple threads we should use always StringBuilder before StringBuffer.

Tagged on:

Leave a Reply

Your email address will not be published. Required fields are marked *


eight − = 4

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>