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;
s.Start();
for (var i = 0; i < n; i++)
result = (p.FirstName + ” ” + p.LastName);
s.Stop();
cElapsedMilliseconds = s.ElapsedMilliseconds;
cElapsedTicks = s.ElapsedTicks;
s.Reset();
s.Start();
for (var i = 0; i < n; i++)
result = string.Format(“{0} {1}”, p.FirstName, p.LastName);
s.Stop();
fElapsedMilliseconds = s.ElapsedMilliseconds;
fElapsedTicks = s.ElapsedTicks;
s.Reset();

Console.Clear();
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”);
Console.Read();

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.

How to install Infobright on Ubuntu

Recently, I have to investigate an opensource data warehousing software and my first choice is Infobright (http://www.infobright.org/).

The installation process is pretty simple except one error:

/usr/bin/strings: ‘/lib/libc.so.6’ : No such file

This can be solved easily by creating a symlink to the location of the file

1. Locate the file

locate libc.so.6

2. Create a symbolic link

# ln -s /path/to/libc.so.6 /lib/libc.so.6

Then you need to have MySql (if you haven’t had one)

> sudo apt-get install mysql-server

Installation process

1. Download the deb file from infobright website: http://www.infobright.org/

2. Make sure you are root

> su –

3. Navigate to a directory, ie /usr/local

# cd /usr/local

4. Install infobright

# dpkg -i /path/to/file.deb

5. Make sure that infobright is working properly.

More information about infobright can be found at http://www.infobright.org/wiki/

How to use VBA in MS Word or MS Excel with MS Access Database?

Assuming that you have already had a MS Access Database setup and create a query.

1.    Make sure that Microsoft DAO Object Library is active. This can be done by go to Tools > References and check the box. In this case, I’m using Microsoft DAO 3.6 Object Library
2.    Create a sub

Sub test()

End sub

3.    Connect to the database

Dim DB As Database
Set DB = OpenDatabase(“c:\path\db.mdb”)

4.    Query the database (in this case is to call a query from the database

Dim Qd As QueryDef
Dim RS As Recordset
Set Qd As DB.QueryDefs(“myQueryName”)
Set RS = Qd.OpenRecordset()
Qd.Close

5.    Loop through the result

Do While Not RS.EOF
MsgBox RS.Fields(0).Value
RS.MoveNext
Loop
RS.Clode

6.    Passing Parameter

Qd.Parameters(0) = “param”

VBA101: A practical guide for VBA user

This post discusses how to write VBA Conditional Statements and how to write VBA Loops

How to write VBA Conditional Statements

If … Else If … Else

If a = b Then
‘ do something
Else If c <> d Then
‘ do another thing
End If

Select Case

Select Case x
Case Is 1
y = 1
Case Is 2
y = 2
Case Else
y = 0
End Select

How to write Various VBA Loops

Do … Loop Until …

Do
‘ do something
Loop Until x < y

Do While … Loop

Do While x < y
‘ do something
Loop

Do While Not … Loop

Do While Not x < y
‘ do something
Loop

For … Next Loop

Dim i As Integer
For i = 1 To 5
‘ do something
Next i

For … Next Loop with Step

Dim i As Integer
For i = 10 to 1 Step -1
‘ do something
Next i

For Each … in … Next Loop

For Each x in xList
‘ do something
Next x

Loop and GoTo (NOT RECOMMENDED)

Dim j As Integer
For i = 0 To 5
b:
If (j = 3) Then GoTo a:
j = i
Next i
a:
j = 4
GoTo b: