The myth of String.Format in .net

by Admin 15. April 2012 07:44

Recently I was talking to a colleague of mine and the subject of string concatenation came up.  Now it is well known that for large string concatenation you should always use the StringBuilder object as it performs better than string concatenation.  But we were talking on a more micro level.  What is better for creating small strings String.Format or string concatenation?  I had run into the problem in the past and I knew string concatenation out performs String.Format hands down, but prior to that I was under the illusion Sting.Format was more efficient.  When I found out my colleague was under the same misconception I decided to write this article.

This article is not saying do not use String.Format.  Sometimes readability and maintainability outweigh small performance gains. Also, for formatting Dates, Times, Numbers, etc…  it is still the best choice.   However if you need to create thousands of small strings (Address concatenation, First Name Last Name, etc.) this may prove to be an eye opener.

So let’s look at the numbers for small string concatenation.  (These numbers are running on my machine your results may vary.)

Creating multiple single strings:

Creating 10000 separate sentences with string format.
Constructed Data: The quick, brown fox jumped over the lazy dog.
Elapsed Time: 00:00:00.0083951
 
Creating 10000 separate sentences with string concatenation.
Constructed Data: The quick, brown fox, jumped over the lazy dog.
Elapsed Time: 00:00:00.0000320
 
Creating 10000  separate sentences  with string builder.
Constructed Data: The quick, brown fox, jumped over the lazy dog.
Elapsed Time: 00:00:00.0011135

Obviously the clear winner here is string concatenation only taking .3 milliseconds.  But look at the difference between String.Format and StringBuilder.  Even with the cost of object creation in the string builder it is 754% faster than String.Format, huh…

Now let’s look at creating large strings:

Creating a 10000 string paragraph with string format.
Constructed Data: The quick, brown fox jumped over the lazy dog.  The quick, brown fox jumped over the lazy dog.  The ...
Elapsed Time: 00:00:05.2135822
 
Creating a 10000 string paragraph with string concatenation.
Constructed Data: The quick, brown fox, jumped over the lazy dog.  The quick, brown fox, jumped over the lazy dog.  Th...
Elapsed Time: 00:00:02.5202178
 
Creating a 10000 string paragraph with string builder.
Constructed Data: The quick, brown fox, jumped over the lazy dog.  The quick, brown fox, jumped over the lazy dog.  Th...
Elapsed Time: 00:00:00.0012484
 

This is where StringBuilder really shines.  There is no difference between creating a large concatenated string and 10000 small strings (results may vary slightly). However, String.Format is still the clear loser taking 207 % longer to complete then string concatenation.  This is when I decided it was time to find out why; ILSpy to the rescue.  I looked at the String.Format definition in mscorlib:

[SecuritySafeCritical]
public static string Format(IFormatProvider provider, string format, params object[] args)
{
	if (format == null || args == null)
	{
		throw new ArgumentNullException((format == null) ? "format" : "args");
	}
	StringBuilder stringBuilder = new StringBuilder(format.Length + args.Length * 8);
	stringBuilder.AppendFormat(provider, format, args);
	return stringBuilder.ToString();
}

WHAT THE HECK !?!?!?!?!  Every time I call string format a new string builder is created?  That is crazy…  But there you have it, code doesn’t lie.

So the next time you are thinking "I’ll use String.Format for performance reasons," think again.

Try it for yourself: 

StringConcat.zip (26.73 kb)

Tags: ,

CLR | Performance

Calendar

<<  June 2017  >>
MoTuWeThFrSaSu
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

View posts in large calendar

Page List

RecentComments

None