<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d6566853\x26blogName\x3d1%25+inspiration\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dBLUE\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttp://patke.blogspot.com/search\x26blogLocale\x3den\x26v\x3d2\x26homepageUrl\x3dhttp://patke.blogspot.com/\x26vt\x3d8220196945898414734', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>
Archives
Subscribe


Wednesday, August 04, 2004

Hmmm... I am working with .net strings a bit at the moment. In particular string concatenation. Every developer worth his salt knows that when doing string concatenation you need to use a StringBuilder (or StringBuffer in java). But what I wanted to know was what would happen if you used string concatenation within a StringBuffer. Something like...

StringBuffer sb = new StringBuffer(50);
sb.Append("Hello" + " World!");

or...

String s = "";
s += "Hello" + " World!";

Now, before I do my little experiement, I think it is worth saying that I thought these two statements would perform about the same (badly) given that the code "Hello" + " World!" is adding one string object to another - by overriding the + operator on the string object.
But I thought it was a hypothesis worth testing, so I found this program on the internet and modified it to...

using System;
using System.Text;
namespace StringBuilderSamp
{
class Class1
{
static void Main()
{
// Concatenation using string
Console.WriteLine("String routine");
string str = string.Empty;
DateTime startTime = DateTime.Now;
Console.WriteLine("Start time:" + startTime.ToString());
int len = 400000;
StringBuilder builder = new StringBuilder(len * 6);
for(int i=0; i less len * 40; i++) //40 appends for every loop iteration.
{
builder.Append(i.ToString());
}
DateTime stopTime = DateTime.Now;
Console.WriteLine("Stop time:" + stopTime.ToString());

// Concatenation using StringBuilder
Console.WriteLine("StringBuilder routine");
startTime = DateTime.Now;
Console.WriteLine("Start time:" + startTime.ToString());
StringBuilder newBuilder = new StringBuilder(len * 6);
for(int i=0; i less len; i++)
{
newBuilder.Append(i.ToString() + i + i + i + i + i + i + i +
i+ i + i + i + i+ i + i + i + i+ i + i +
i + i+ i + i + i + i+ i + i + i + i+ i +
i + i + i); // 40 concats
}
stopTime = DateTime.Now;
Console.WriteLine("Stop time:" + stopTime.ToString());
Console.WriteLine("\r\n\r\nContinue...");
Console.ReadLine();
}
}
}

Now the fun begins - I won't hold you in suspense. The results of this program are...

String routine
Start time:04/08/2004 13:13:16
Stop time:04/08/2004 13:13:26
StringBuilder routine
Start time:04/08/2004 13:13:26
Stop time:04/08/2004 13:13:36

Continue...

So they performed the same! I guess it is not the internal concatenation of strings within the string object that is time consuming but the "reseting" of the origional object.

Really good stuff. Hats off to the boys and girls and Microsoft for writing software a bit above the notch.


Permalink
Comments: Post a Comment