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.

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()

5.    Loop through the result

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

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 something
Loop Until x < y

Do While … Loop

Do While x < y
‘ do something

Do While Not … Loop

Do While Not x < y
‘ do something

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


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

VBA101: A practical guide for VBA user

This post contains 2 parts: How to declare VBA Variables and How to write VBA “Functions”

How to declare VBA Variables

Normal Variables

Dim myVar As String
myVar = “Hello World”
Dim myObj As Object
Set myObj = anotherObj

Class” Variables

Private x As String
Sub myVar()
x = “Hello World”
End Sub

How to write VBA “Functions”

No parameter, No return

Sub MySub()
‘ do something
End Sub

Parameter, No return

Sub MySub(x As Integer)
‘ do something
End Sub
MySub myVar

More than one Parameter, No return

Sub MySub(x As Integer, y As Integer)
‘ do something
End Sub
MySub myVar, myAnotherVar

Optional Parameter, No return

Sub MySub(x As Integer, y As Integer, Optional z As Integer)
If IsMissing(z) Then
‘ do something
End If
End Sub
MySub myVar, myAnotherVar
MySub myVar, myAnotherVar, myOptionalVar

No parameter, Return

Function MySub() As String
‘ MySub is automatically returned
End Function
Str = MySub

Pass By Values/By Reference

Sub MySub(ByVal x As Integer, ByRef y As Integer)
‘ do something
End Sub
MySub myVar, myAnotherVar

VBA101: A practical guide for VBA user

I wrote this guide a few months ago (September, 2010) and I think it might be something useful for people, who are interested in VBA (Visual Basic for Applications).

Things to know

  1. VBA stands for Visual Basic for Applications
  2. VBA only works with Microsoft Office for Windows
  3. VBA allows user to programmatically use Microsoft Office
  4. Press Alt+F11 to jump to Visual Basic Editor view

VBA can …

  1. Automatically update the document
  2. Connect to database and populate information
  3. Do Forms

How to write a Hello World in VBA

Sub MyHelloWorld()
MsgBox “Hello World”
End Sub

Note about VBA

  1. Comment is ‘
  2. No ; at the end of the line
  3. No {} for block code
  4. Use Dim to declare variable
  5. Assigning values to Primitive types do not require Set