Click here to Skip to main content
15,123,509 members
Articles / Programming Languages / C#
Posted 8 May 2005


51 bookmarked

Performance considerations for strings in C#

Rate me:
Please Sign up or sign in to vote.
4.41/5 (31 votes)
8 May 2005CC (ASA 2.5)5 min read
The effects of string handling on performace.


How you handle strings in your code can have surprising effects on performance speed. In this article, I shall look at two of the common issues that using strings can produce: use of temporary string variables and string concatenation.


There comes a time in every project when you have to start looking at coding standards. Using FxCop is a good place to start. My favourite set of FxCop rules is the 'Performance' set.

So there I was, checking my project against FxCop and seeing lots of issues with strings. I must admit something: I have always had problems with C#'s immutable strings. When I see myString.ToUpper(), I always forget that it won't change the contents of myString but will return a new string entirely (this is because strings are immutable in C#).

I proceeded to fix my code to remove FxCop's warnings and then I noticed something - my code was faster. I decided to investigate and ended up writing the test code that I present here.

Using the code

The test code is very simple. A console application calls four test methods. Each method performs a string processing routine 1000 times (so the time to execute is nice and long to look at performance differences).

The four test methods are split into two groups of two. The first group compares case-insensitive string comparison.

String Comparison and Temporary String Creation

The first test routine is a bad case-insensitive string comparison. The routine for the comparison is:

static bool BadCompare(string stringA, string stringB) 
    return (stringA.ToUpper() == stringB.ToUpper());

For this code, FxCop shows the following advice:

"StringCompareTest.BadCompare(String, String):Boolean calls 
   String.op_Equality(String, String):Boolean after converting 'stack1', a local, 
   to upper or lowercase. If possible, eliminate the string creation and call the 
   overload of String.Compare that performs a case-insensitive comparison."

What this means is that each call to ToUpper() is creating a temporary string which has to be created and managed by the garbage collector. This takes extra time and uses more memory. The String.Compare method is more efficient.

The second test routine uses String.Compare:

static bool GoodCompare(string stringA, string stringB)
    return (string.Compare(stringA, stringB, true, 
         System.Globalization.CultureInfo.CurrentCulture) == 0);

This method prevents the creation of unnecessary temporary strings.

According to nprof, the Good Comparison takes 1.69% of the total execution time of the code, while the Bad Comparison takes 5.50% of the total execution time.

So the String.Compare method is over three times as fast as the ToUpper method. If you have code that is performing a lot of string comparisons (especially in a loop) then using String.Compare can make a big difference.

String Concatenation inside a loop

The final pair of test routines consider string concatenation within a loop.

The 'bad' test routine is as follows:

static string BadConcatenate(string[] items)
    string strRet = string.Empty;

    foreach(string item in items)
        strRet += item;

    return strRet;

When FxCop sees this code, it is so outraged that it even marks the broken rule in red! FxCop says the following:

"Change StringCompareTest.BadConcatenate(String[]):String to use StringBuilder 
  instead of String.Concat or +="

The 'good' test routine was written as follows:

static string GoodConcatenate(string[] items)
    System.Text.StringBuilder builder = new System.Text.StringBuilder();

    foreach(string item in items)

    return builder.ToString();

This is an almost archetypal example given for the use of the System.Text.StringBuilder class. The issue with the bad example is the creation of more temporary strings. Because strings are immutable, the concatenation operator (+=) actually creates a new string out of the two originals and then points the original string instance at the new string.

However, when we look at performance, according to nprof, the we find that the 'Bad' concatenation takes 5.67% of the total execution time, while the 'Good' concatenation takes 22.09%. I'll run that by you again:

Using StringBuilder took almost four times longer than simple string concatenation!


The answer is partly in the design of the test; the concatenation routines only concatenate ten short strings. The StringBuilder class is a more complex class than a simple immutable string, so creating one StringBuilder is more expensive in performance than doing ten simple string concatenations.

I repeated the test with differing numbers of string concatenations, and found the following results:

Chart of concatenation method effect on relative performance.

Note: The values shown here are the % of the total execution time taken by the test routines. The 'Good Concatenation' test is not actually getting faster, but takes less relative time than the 'Bad Concatenation' routine.

So, it would seem that the StringBuilder class is only really faster if you are concatenating more than about 600 strings.

Of course, the other reason for the use of the StringBuilder class is memory allocation. Using the CLRProfiler produced the following memory use timeline for concatenation of 100 simple strings:

Memory usage timeline.

The section marked 'A' shows the effect of the bad string concatenation routine on memory allocation and de-allocation. The maximum allocated memory is increasing rapidly and there is a high number of garbage collections occurring (roughly 215 collections for this section).

The section immediately following the 'A' section shows the memory profile for the good string concatenation routine. The maximum allocated memory is increasing less rapidly and there are far fewer garbage collections being made (roughly 60 collections for this section).

So using the StringBuilder class may not be faster in some cases, but it is kinder to the garbage collector.


Use the String.Compare method for case-insensitive string comparison. It's just faster. Nice and simple.

Use the StringBuilder class for speed increases only if you are concatenating more than about 600 strings within a loop. The caveat here is that the length of the strings you are manipulating may also affect the speed tradeoff, as may the effects on the Garbage Collector so you should really perform your own tests for your specific code.

Points of Interest

I was surprised at what a difference using the correct string manipulation methods made to code in the real world (although we do perform a lot of string comparisons and concatenations in my current project).

FxCop's performance rules are a good starting point for finding potentially slow code which can direct you to some easy fixes to improve code performance. Both of the issues discussed here are marked by FxCop as 'non-breaking' which means that the changes should not break any code depending on the code changed. This should be a no-brainer: a non-breaking change for performance improvements should always be made.


  • April 2005 - First draft of the article.


This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


About the Author

Dr Herbie
Team Leader
United Kingdom United Kingdom
After ten years studying biology and evolution, I threw it all away and became a fully paid up professional programmer. Since 1990 I have done C/C++ Windows 3.1 API, Pascal, Win32, MFC, OOP, C#, etc, etc.

PS. In the picture, I'm the one in blue. On the right.

Comments and Discussions

SuggestionEven faster string comparison! Pin
Pavel Vladov11-Sep-12 2:48
MemberPavel Vladov11-Sep-12 2:48 
GeneralBad Compare : FxCop Rule Pin
Member 403042023-Dec-09 20:28
MemberMember 403042023-Dec-09 20:28 
GeneralRe: Bad Compare : FxCop Rule Pin
Dr Herbie24-Dec-09 6:52
MemberDr Herbie24-Dec-09 6:52 
GeneralRe: Bad Compare : FxCop Rule Pin
Member 403042029-Dec-09 20:26
MemberMember 403042029-Dec-09 20:26 
GeneralMemory Usuage Additional Tests Pin
Cumps16-Sep-07 8:51
MemberCumps16-Sep-07 8:51 
GeneralI like your article Pin
L8N8Hack19-Jul-07 6:21
MemberL8N8Hack19-Jul-07 6:21 
GeneralPerhaps an incomplete conclusion Pin
karl beyer19-Sep-06 6:19
Memberkarl beyer19-Sep-06 6:19 
QuestionHelp with String Variable Comparision Pin
mpham10-Aug-06 5:10
Membermpham10-Aug-06 5:10 
GeneralThis artical is not acurate Pin
Yang Yu2-Jan-06 0:56
MemberYang Yu2-Jan-06 0:56 
AnswerRe: This artical is not acurate Pin
Dr Herbie4-Jan-06 2:48
MemberDr Herbie4-Jan-06 2:48 
GeneralPlease Help Pin
akorolev1021-Oct-05 2:04
Memberakorolev1021-Oct-05 2:04 
GeneralRe: Please Help Pin
Dr Herbie21-Oct-05 2:27
MemberDr Herbie21-Oct-05 2:27 
GeneralRe: Please Help Pin
illium3-Jul-06 13:48
Memberillium3-Jul-06 13:48 
GeneralRe: Please Help Pin
Dr Herbie3-Jul-06 22:50
MemberDr Herbie3-Jul-06 22:50 
GeneralAnother article with the same result Pin
Dr Herbie28-Sep-05 23:23
MemberDr Herbie28-Sep-05 23:23 
QuestionWhat's wrong with my test? Pin
milos.koutny16-May-05 8:44
Membermilos.koutny16-May-05 8:44 
AnswerRe: What's wrong with my test? Pin
Dr Herbie31-May-05 22:59
MemberDr Herbie31-May-05 22:59 
GeneralRe: What's wrong with my test? Pin
milos.koutny2-Jun-05 5:59
Membermilos.koutny2-Jun-05 5:59 
GeneralRe: What's wrong with my test? Pin
Dr Herbie2-Jun-05 6:25
MemberDr Herbie2-Jun-05 6:25 
GeneralVery Nice Indeed Pin
mrchief_200012-May-05 5:09
Membermrchief_200012-May-05 5:09 
GeneralMicro Benchmark Pin
MattHope11-May-05 23:23
MemberMattHope11-May-05 23:23 
GeneralMore points Pin
Uri Lavi11-May-05 10:02
MemberUri Lavi11-May-05 10:02 
GeneralAbout GoodCompare Pin
Gonzalito10-May-05 15:13
MemberGonzalito10-May-05 15:13 
GeneralString.Concat Pin
WellerEE10-May-05 4:48
MemberWellerEE10-May-05 4:48 
GeneralRe: String.Concat Pin
Dr Herbie10-May-05 5:32
MemberDr Herbie10-May-05 5:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.