C# String Concatenation Performance

Out of curiosity, I’m trying to find out which string concatenation method would be the fastest (most optimised) when writing C# applications.

There are a few ways to do that.

var str = “Foo” + ” ” + “Bar”;

var str = string.Format(“{0} {1}”, “Foo”, “Bar”);

StringBuilder strBuilder = new StringBuilder();
strBuilder .Append(“Foo”);
strBuilder .Append(” “);
strBuilder .Append(“Bar”);
var str = strBuilder .ToString();

It is quite clear at StringBuilder adds extra overhead for the concatenation. Therefore, I’m going to demonstrate only the performance between the first two options.

This example uses the code from StackOverflow.

Stopwatch s = new Stopwatch();
var p = new { FirstName = “Bill”, LastName = “Gates” };
int n = 1000000;
long fElapsedMilliseconds = 0, fElapsedTicks = 0, cElapsedMilliseconds = 0, cElapsedTicks = 0;
string result;
for (var i = 0; i < n; i++)
result = (p.FirstName + ” ” + p.LastName);
cElapsedMilliseconds = s.ElapsedMilliseconds;
cElapsedTicks = s.ElapsedTicks;
for (var i = 0; i < n; i++)
result = string.Format(“{0} {1}”, p.FirstName, p.LastName);
fElapsedMilliseconds = s.ElapsedMilliseconds;
fElapsedTicks = s.ElapsedTicks;

Console.WriteLine(n.ToString() + ” x result = string.Format(\”{0} {1}\”, p.FirstName, p.LastName); took: ” + (fElapsedMilliseconds) + “ms – ” + (fElapsedTicks) + ” ticks”);
Console.WriteLine(n.ToString() + ” x result = (p.FirstName + \” \” + p.LastName); took: ” + (cElapsedMilliseconds) + “ms – ” + (cElapsedTicks) + ” ticks”);

The result

  • String Concatenation: 61ms – 204,570 ticks
  • String Format: 173ms – 574,676 ticks

I guess this might be a little bit overkill and I also like a comment from someone (sorry, I really can’t remember who that was) that … at the end of the day, machines are more powerful and it’s best to make the code readable.

So, if you are curious like me, feel free to run the code and experiment this yourself. Credit: StackOverflow!


Entity Framework and View

Entity Framework is a really useful way for .NET developers to interact with databases. Recently, I have been working on implementing oData using WCF(Windows Communication Foundation).

A quick overview on oData:

Since, data in the data warehouse contains sensitive and too much details. It is necessary in this case to create a small subset of data using View. The fun begins with Entity Framework seems to have issues when creating and updating the data structure. Common errors are:

“1. The table/view TABLE_NAME does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and uncomment it”

This can be resolved easily. For example, if you Table Primary Key column is “ID”, in your View, you have to add this code, ISNULL(ID, – 1) AS ID.

2. Unable to update the EMDX – View mapping errors.

This is a little bit more complicated and workaround is found by manually editing the EMDX file using the XML Editor.

  1. Remove the <DefiningQuery>…<DefiningQuery>
  2. Remove store:Name=”XXXX” attribute from the <EntitySet> tag
  3. Remove the store prefix from the store:Schema attribute in the <EntitySet>

Now you can update your model again.