Chinh Do

StringBuilder is not always faster – Part 1 of 2

First time here? Check out my greatest hits or look around in the archives, and consider subscribing to the latest posts via RSS or email. I am also on Twitter and Google Plus. Thanks for visiting.
24th February 2007

StringBuilder is not always faster – Part 1 of 2

How often have you been told to use StringBuilder to concatenate strings in .NET? My guess is often enough. Here is something you may not know about string concatenation: StringBuilder is not always faster. There are already many articles out there that explain the why’s, I am not going to do that here. But I do have some test data for you.

When concatenating three values or less, traditional concatenation is faster (by a very small margin)

This block of code took 1484 milliseconds to run on my PC:

for (int i = 0; i <= 1000000; i++) 
{ 
    // Concat strings 3 times using StringBuilder 
    StringBuilder s = new StringBuilder(); 
    s.Append(i.ToString()); 
    s.Append(i.ToString()); 
    s.Append(i.ToString()); 
}

And this one, using traditional concatenation, took slightly less time (1344 milliseconds):

for (int i = 0; i <= 1000000; i++) 
{ 
    // Concat strings 3 times using traditional concatenation 
    string s = i.ToString(); 
    s = s + i.ToString(); 
    s = s + i.ToString(); 
}

The above data suggests that StringBuilder only starts to work faster once the number of concatenations exceed 3.

Building strings from literals

When building a large string from several string literals (such as building a SQL block, or a client side javascript block), use neither traditional concatenation nor StringBuilder. Instead, choose one of the methods below:

+ operator

// Build script block 
string s = "<script>" 
       + "function test() {" 
       + "  alert('this is a test');" 
       + "  return 0;" 
       + "}";

The compiler concatenates that at compile time. At run-time, that works as fast as a big string literal.

@ string literal

I sometimes use the @ string literal which allows for newlines (I find this syntax is harder to maintain, formatting-wise, than using the + operator):

string s = @"<script> 
        function test() { 
        alert('this is a test'); 
        return 0; 
        }";

Both methods above run about 40 times faster than using StringBuilder or traditional string concatenation.

Rules of Thumb

  • When concatenating three dynamic string values or less, use traditional string concatenation.
  • When concatenating more than three dynamic string values, use StringBuilder.
  • When building a big string from several string literals, use either the @ string literal or the inline + operator.

Updated 2007-09-29

I have posted a follow-up article to provide more detailed analysis and to answer some of the questions asked by readers.

posted in Dotnet/.NET - C#, Programming | 22 Comments

17th February 2007

Google Maps on Your Windows Mobile Phone

Google has just made available Google Maps for Windows Mobile. Here’s the blurb on Google Maps Mobile Page:

Take the power of Google Maps with you on your mobile phone.

Real-time traffic — See where the congestion is, and estimate delays in over 30 major US metropolitan areas.

Detailed directions — Whether you plan to walk or drive, your route is displayed on the map itself, together with step-by-step directions.

Integrated search results — Local business locations and contact information appear all in one place, integrated on your map.

Easily movable maps — Interactive maps let you zoom in or out, and move in all directions so you can orient yourself visually.

Satellite imagery — Get a bird’s eye view of your desired location. (It’s like you’re there, we swear.

I tried the app on my Samsung i730 Windows Mobile phone and it worked great. Map data is retrieved from Google’s servers as needed just like regular Google Maps. Response time is slower than on a desktop but still very usable on my Verizon EDVO connection.

If you are a user of Google Maps then the screen below will look familiar to you:

Google Maps

Note the transparent buttons. Great idea.

Searching for local businesses is a snap. Click Search . Type in the name of the busines. Hit OK. Search matches are displayed on the map as pushpins:

Google Maps

There is also the Satellite View, but missing is the Hybrid View.

Google Maps

More info here.

posted in Gadgets, Reviews, Software/tools, Technology, Windows Mobile / Pocket PC | 2 Comments

17th February 2007

Google Co-op

Google Co-op allows you to create customized search engines to fit your search interests. .NET developers, you may want to give the following pre-customized search engines a try:

For me, Co-op is a good idea but for now I will stick with just regular Google, at least for .NET related searches. I gave SearchDotNet.com a try and was initially underwhelmed my the number of matches. For example, searching for “EntryAssembly” using the DotNetSearch engine above yielded two pages of matches. Regular Google returns 387 pages.

posted in Dotnet/.NET - C#, Programming, Reviews, Software/tools | 2 Comments

12th February 2007

Debugging Oracle PL/SQL from Visual Studio

I have not tried this myself but there is now a way to debug Oracle PL/SQL from Visual Studio. Have you tried it?

posted in Dotnet/.NET - C#, Programming | 0 Comments

8th February 2007

Test Your Web Site in Internet Explorer 6 Using Microsoft IE6 Virtual Test Image

We all know that it’s a good idea to test our web pages in all popular browsers (to me that’s the various current versions of Internet Explorer and Firefox). It’s also a good idea to back up your data frequently, to listen to your elders, to not take a bath right after dinner, etc. Ok, so I admit I didn’t test my latest WordPress theme in IE6. While browsing the web on a friend’s PC, I discovered that my blog did not render correctly in IE6. It was broken in a major way. The right side bar completely disappears. Ouch!

I needed a way to test my site in Internet Explorer 6 at home, and since I have upgraded all of my PC’s at home to IE7, it seems like it’s a good time to give “virtual appliances” a try.

It took me about an hour to download the Internet Explorer 6 Application Compatibility VPC Image, install it, and have the virtual machine up and running. It helped that I already had Virtual Server installed. Yes Virtual Server works with Virtual PC disk images just fine. The current IE6 image expires on 4/1/2007 but I am sure Microsoft will replace it with another one when the time arrives. There is a blog entry on IEBlog about the image here.

Now I can reproduce the problem on my PC using IE6. That’s going to be the easy part I am afraid.

IE6

Update (2/8/2007): I fixed the IE6 problem. Apparently I had some sort of error in my sidebar.php file which messed up the HTML code, causing problems with IE6.

posted in Dotnet/.NET - C#, Programming, Software/tools, Technology | 1 Comment

3rd February 2007

E.T. Will Probably Not Find Us in Our Lifetimes

According to this Guardian article, other advanced life forms just have not found us yet because they have not had enough time to look. According to the calculations, it may be a while before we get to say hello to E.T.

An alternative theory that I have is that once a galactic civilization advances past a certain level, it learns about an unwritten rule of the universe. That is growing civilizations are to be left alone untouched?

posted in Science | 11 Comments

  • Calendar

  • February 2007
    M T W T F S S
    « Jan   Mar »
     1234
    567891011
    12131415161718
    19202122232425
    262728