Blog Moved ....

ScottCate.com

community

frenz

my book(s)

my products

String Concatenation Speed?

Is there any hard research about which one of the following is faster, or more scalable?

  1. string name = “Scott“ + “ “ + “Cate“;
  2. StingBuilder sb = new StringBuilder();
    sb.Append(“Scott“);
    sb.Append(“ “);
    sb.Append(“Cate“);
    string name = sb.ToString();
  3. string name = string.Format(“{0} {1}“,“Scott“,“Cate“)
  4. string name = string.Concat("Scott"," ","Cate");

Maybe even another way that I'm not aware of yet?

Comments

Roy said:

Given the example that you show, I would say it does not matter. However, as a general rule, the StringBuilder method is the better way. The overhead of creating the StingBuilder negates any gain by using it (in this specific case).

To know why, then you would need to understand how string concatenation works. Basically, it is done by creating a NEW string object for each concatenation. A StringBuild just uses an internal block of memory and only creates the final string when you call the "ToString()" method.
# February 19, 2004 1:23 AM

Scott Cate, kbAlertz.com said:

Right, I understand string is an immutable object, and needs to be created somewhere, no matter what. I was just wondering in an environment, where strings are dynamically built, like in a custom e-mail application, or something, is there really any hard facts that one way is better then the other.

Personally I'm a fan of stringbuilder and the string.Format() method, but are there facts anywhere to backup, that one is better then the other?
# February 19, 2004 1:45 AM

Wesner Moise said:

For your simple example, #1 and #4 are the same (+ is short for concat) and produce the fastest result.

A single call to concat will be faster than any other method including String.Format. But if you make multiple calls, then stringbuilder scales better--linearly rather than quadratically.
# February 19, 2004 2:51 AM

Fabrice said:

Did someone find the best way to concatenate strings when you need to preprend instead of appending?
I mean in loop you have to do this :
path = parent.Name+"."+path
The StringBuilder is not good at all in this case. What would be the best way? Just using +?
# February 19, 2004 6:03 AM

Scott said:

Fabrice, what about StringBuilder.Insert(index, value)?

-Scott
# February 19, 2004 8:00 AM

Fabrice said:

Scott, StringBuilder.Insert() is very slow. This is because it has to move data from the beginning of the buffer to make place for the string you want to insert. If you repeat this operation multiple times, this is very expensive.
This is not documented in the SDK, it should be !
# February 19, 2004 11:15 AM

Tariq said:

I have done 5 tests (i is the loop variable from 1 to 100,000)

s += i.ToString()

s = s + i.ToString();

String.Format({0}{1}, s, i.ToString())

String.Concat(s, i.ToString());

and StringBuilder.Append(i.ToString())

The results surprised me a little, i expected stringbuilder to be quickest, but it came out in the following order (Fastest to Slowest)

String.Concat 0.031 seconds

StringBuilder 0.046 seconds

String.Format 0.093 seconds

= + 64.265 seconds

+= 64.937 seconds

The top 3 would be insignificant in most programs for loops under 100,000 interations.

I did 20,000 iterations and the top two reversed themselves:

StringBuilder 0.000 seconds

String.Concat 0.015 seconds

String.Format 0.015 seconds

= + 1.843 seconds

+= 1.875 seconds

Next i tried 1,000,000 iterations and it was the order of the second of the two results. but 10,000,000 went back to the first order

This must depend on what the machine is doing at the time. But StringBuilder and String.Concat should be the methods of choice.

String.Concat also allows you to insert an item at the beginning of the string quickly.

# August 3, 2007 10:47 AM

Adam said:

I've done quite a bit of playing around with string concatenation, and I've found string.Concat to be horribly slow.  In my experience, using the addition operand has proven noticably faster.

I also tried comparing StringBuilder vs the addition operand, and StringBuilder proved a little faster.

My project is creating csv files with only 6 values, and I'm noticing substantial differences when creating these files over and over with the various methods.

This with .NET 3.5 and Visual Studio 2008 executing my app in Release mode.

# June 7, 2009 4:34 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)