IanG on Tap

Ian Griffiths in Weblog Form (RSS 2.0)

Blog Navigation

April (2018)

(1 item)

August (2014)

(1 item)

July (2014)

(5 items)

April (2014)

(1 item)

March (2014)

(1 item)

January (2014)

(2 items)

November (2013)

(2 items)

July (2013)

(4 items)

April (2013)

(1 item)

February (2013)

(6 items)

September (2011)

(2 items)

November (2010)

(4 items)

September (2010)

(1 item)

August (2010)

(4 items)

July (2010)

(2 items)

September (2009)

(1 item)

June (2009)

(1 item)

April (2009)

(1 item)

November (2008)

(1 item)

October (2008)

(1 item)

September (2008)

(1 item)

July (2008)

(1 item)

June (2008)

(1 item)

May (2008)

(2 items)

April (2008)

(2 items)

March (2008)

(5 items)

January (2008)

(3 items)

December (2007)

(1 item)

November (2007)

(1 item)

October (2007)

(1 item)

September (2007)

(3 items)

August (2007)

(1 item)

July (2007)

(1 item)

June (2007)

(2 items)

May (2007)

(8 items)

April (2007)

(2 items)

March (2007)

(7 items)

February (2007)

(2 items)

January (2007)

(2 items)

November (2006)

(1 item)

October (2006)

(2 items)

September (2006)

(1 item)

June (2006)

(2 items)

May (2006)

(4 items)

April (2006)

(1 item)

March (2006)

(5 items)

January (2006)

(1 item)

December (2005)

(3 items)

November (2005)

(2 items)

October (2005)

(2 items)

September (2005)

(8 items)

August (2005)

(7 items)

June (2005)

(3 items)

May (2005)

(7 items)

April (2005)

(6 items)

March (2005)

(1 item)

February (2005)

(2 items)

January (2005)

(5 items)

December (2004)

(5 items)

November (2004)

(7 items)

October (2004)

(3 items)

September (2004)

(7 items)

August (2004)

(16 items)

July (2004)

(10 items)

June (2004)

(27 items)

May (2004)

(15 items)

April (2004)

(15 items)

March (2004)

(13 items)

February (2004)

(16 items)

January (2004)

(15 items)

Blog Home

RSS 2.0

Writing

Programming C# 5.0

Programming WPF

Other Sites

Interact Software

String.Concat and Performance

Tuesday 16 March, 2004, 05:59 PM

This is another in the learning new things about stuff I thought I should have known inside out by now series. This time, it's String.Concat. I ask you! How can it be possible not to know everything about such a simple UI?

Maybe I'm the last person on the planet to discover this, but String.Concat actually has 8 overloads, and I only just discovered that one of the overloads takes three strings and concatenates them all at once. Another does the same with four strings.

My gut reaction is something along the lines of: uh, why? It does seem like an overload purely for the benefit of the terminally lazy. However, given the context in which I came across this, it actually makes perfect sense. I was reading Rico Mariani's excellent blog, in which he posts a performance quiz about string concatenation. He presents three possible ways of writing some strings to a stream. While I correctly guessed that option 1 was going to be faster than option 2 (I knew that String.Format is comparatively costly) I was surprised when I read that option 1 only performed a single concatenation operation rather than two. But that's exactly what the three-string overload of String.Concat is for - to allow three strings to be concatenated in one go, avoiding the extra memory allocation that would be required to concatenate the first two and then add the third on.

I can only assume that Microsoft found that enough people write code of the following form:

string s = "Foo: " + value + ", Bar";

that it was worth them adding a couple of special-case overloads and associated compiler optimizations to turn this into a very memory-efficient operation that only involves allocating one new string and performing one concatenation.

So if, like me, you've ever felt guilty at hacking together a string by adding together 3 or 4 individual strings, it turns out that you weren't in fact writing a low-effort, low-performance hack, but were writing an ultra-efficient piece of code of which you can be justly proud. Or something.

I particularly enjoyed one of Rico's summary comments about the performance quiz he set on this topic:

Some thought the problem was very easy. Many of these people didn't do very well :)

Copyright © 2002-2013, Interact Software Ltd. Content by Ian Griffiths. Please direct all Web site inquiries to webmaster@interact-sw.co.uk