BlackWaspTM
Performance
.NET 4.0

Speed Test: String Concatenation

C# and the .NET framework provide several ways in which strings may be concatenated by appending the contents of one string to those of another. Some methods use immutable strings directly, others use mutable data to achieve faster performance.

Test Description

Purpose

This test was designed to compare the execution time several of the ways that the C# programming language and the .NET framework provide to enable strings to be concatenated. As strings are immutable, combining two strings creates a third string, which must be initialised and allocated some memory. If you are performing concatenation within a loop, as the number of concatenation operations grows, the number of string references also increases.

The StringBuilder class claims to remove these problems and increase performance. The performance tests described in this article put this claim to the test and attempt to compare the relative times of the string concatenation operator (+), the String.Concat method and the Append method of the StringBuilder class.

Test Process Functionality

It was important that the speed tests for the three concatenation operations each gave the same end result. In each test a string was created by repeatedly appending a single character to an existing value. In the case of the StringBuilder, this was achieved by appending a single character to the StringBuilder instance. Additionally, the StringBuilder was converted to a string at the end of the operation, so the results were all of the same type.

The results were expected to be different depending upon the number of concatenations performed. For this reason, each operation was performed once, to give a result containing two characters, five times, fifty times, five hundred times and one hundred thousand times. This gave a total of fifteen tests.

Timing

The timing of the tests was controlled automatically using the Stopwatch class. All of the tests were performed at least one hundred times and the mean average of the results calculated.

Test Conditions

Hardware

The test results included below are those produced using an AMD Athlon64 X2 dual core processor running at 1.9GHz with 4GB RAM. These tests are indicative of further relative test results that were carried out on a variety of equipment

The tests were executed using two operating systems, each with the latest service packs and patches. These were:

  • Windows Vista Home Premium
  • Windows 7 Enterprise

In each test, the software was compiled as a .NET framework 4.0 console application. All non-essential services were stopped to limit the risk of interference.

Results

This table shows the average timings for the three operations. The five numeric columns show the average times for 1, 5, 50, 500 and 100,000 concatenations. Note that the first four columns show results measured in microseconds. The final column, where 100,000 characters were combined, is shown in milliseconds so should be multiplied by one thousand for comparison with the values for a lower number of iterations.

Concatenation Operations
1550500100,000
+ Operation3.6μs5.2μs24.4μs711.0μs14,980ms
String.Concat()3.8μs5.0μs24.6μs704.1μs14,920ms
StringBuilder.Append()4.1μs4.8μs8.5μs34.9μs0.5ms
29 August 2011