By Michael Balloni
StrCat.Catter - An Efficient String Concatenation Component
I wrote this component -
StrCat.Catter - because string concatenation in
Visual Basic (Script) has poor performance characteristics. I found that code like
performed very poorly. The code took a long time to execute, during which time the processor was totally saturated. I found articles on MSDN stating that this type of code was a bad idea, and I've heard that this type of concatenation results in run times that are proportional to the square of the number of concatenations. There are times when a solution based on passing strings - instead of passing function pointers or function objects capable of outputting strings - is favorable. For example, let's say I have a general routine that asks the user to verify their billing information before they make a purchase, and this routine needs to output a summary of what the user is buying. You might be tempted to say:
but what if your billing information verification code needs to sprinkle HTML on boths sides of the purchase summary. Then you're better off passing that purchase summary - as a string - to the billing information verification code. You could put together some sort of "object that outputs purchase summaries when this method is called" type of system - and VBScript allows for this type of system - but let's keep things simple and pretend we just want to pass in a string and we need efficient string concatenation.
Catter" is a C++ COM component with a
very small interface. Here's typical usage of it:
That's pretty simple, isn't it? By having the concatenation method be the default
method, you just say the Catter's name - in this case
strcat - and it just
tacks on the string you give it. When you're ready to use the whole string, call
Dump and you've got it!
In Part 2 we'll look at the implementation of our
Catter class and performance tests.