Published: Monday, September 25, 2000
StrCat.Catter - An Efficient String Concatenation Component, Part 2
By Michael Balloni
Read Part 1
In Part 1 we looked at the motivation for
StrCat.Catter. In this part we'll look at the implementation and some
performance results.
Interface
For those who know IDL - or can fake it - Catter's interface is as follows:
[id(0), helpstring("Add a string to the end of this. Like str = str & .")]
HRESULT Cat([in] BSTR strToAdd);
[id(1), helpstring("Compute the total string for this.")]
HRESULT Resolve([out, retval] BSTR* pStrOutput);
[id(2), helpstring("Empty the contents of this.")]
HRESULT Reset();
[propget, id(3), helpstring("Get the current length of the total string. Like Len, but much faster.")]
HRESULT Length([out, retval] int *pVal);
[id(4), helpstring("Add a string to the front of this. Like str = & str.")]
HRESULT Prepend([in] BSTR strToPrepend);
[id(5), helpstring("Surround the current contents with a before and after string. Like str = & str & .")]
HRESULT Surround([in] BSTR strToPrepend, [in] BSTR strToAppend);
[id(6), helpstring("Dump = Resolve then Reset. Returns the total strings, and empties the contents of this.")]
HRESULT Dump([out, retval] BSTR* pStrOutput);
[id(7), helpstring("Synonym for Cat. Like str = str & .")]
HRESULT Append([in] BSTR strToAdd);
|
As you can see, this is general-purpose string assembly engine. At first there was
just Cat and Resolve. Then I found that I needed
Length, Prepend, and Surround. I mostly use
Dump instead of Resolve because it frees resources sooner.
Append and Reset are included for completeness.
Usage
I like to use this component for concatenation of SQL query strings, both in VB and in
ASP scripts. SQL Server devours big batches of SQL much better than lots of little
requests. Since Catter is so efficient, great gains can be had when the
size of the strings and the number of concatenations grows. As an example, I use this
component to manufacture SQL that marks all of a set of database-modelled files and
folders and their children folders and files as public or private. If there are just a
few files and folders, you can get away with VB string concatenation. Throw in a few
thousand files and deeply nested folders, and you need the improved performance of
Catter.
Performance
Catter outperforms standard VB string concatenation after several dozen concatenations. Here's
The results can be seen below. If you're interested in the code tests I used, check
out the VB test code used. Note that
is employs the use of a free component, ASP
Profiler. To run this code you will need to download and install the component.
| Concatenations | Length of String | VBScript Concatenation |
StrCat.Catter Concatenation |
| 1 | 44 |
0.01 | 0.05 |
| 5 | 44 |
0.02 | 0.06 |
| 10 | 44 |
0.02 | 0.08 |
| 25 | 44 |
0.05 | 0.31 |
| 50 | 44 |
0.14 | 0.35 |
| 100 | 44 |
1.07 | 0.44 |
| 200 | 44 |
1.86 | 0.81 |
| 500 | 44 |
12.19 | 2.30 |
| 1000 | 44 |
140.20 | 5.51 |
| 2000 | 44 |
940.53 | 12.41 |
|
| 200 | 484 |
150.13 | 11.31 |
| 500 | 484 |
1,467.19 | 25.16 |
| 1000 | 484 |
6,414.08 | 58.50 |
| 2000 | 484 |
26,419.20 | 112.30 |
Times in milliseconds
You can see that it takes about three times longer to get Catter up to speed
for a single concatenation, and even after a few dozen Catter lags behind.
But once VB starts to slow down, it does so disastrously - quadratically, in fact - and
Catter keeps its cool, staying almost linear in terms of time versus
concatenations. I'd hate to be the person waiting 25 seconds for VB to concatenate some strings!
Implementation
Catter is an STL vector of STL wstrings. Catter's implementation
calls on the reserve methods of both the vector and wstring classes. The implementations
of these classes are designed so that their execution time stays linear under these
circumstances. They accomplish this by doubling their allocated memory when they need
to grow, thereby avoiding making too many allocations and fragmenting the heap.
Conclusions
When you need to concatenate more than a few dozen strings, consider using
StrCat.Catter. Catter's optimized C++ implementation can
prevent your site from CPU saturation when folks start asking more of it than VB can
handle.
Happy Programming!
By Michael Balloni
Attachments:
Download the StrCat.Catter source code and DLL in ZIP format
View the VB test code used for the performance tests
Shameless Plug
StrCat.Catter and many other COM components power
Streamload.com, a digital entertainment
delivery site offering unlimited free online storage - no catch: upload all your files
from your computer - and a bevy of media-savvy services including compression (ZIP-ing),
quality conversion (BMP -> JPEG, watermarking), de-digitization (CD burning, disk magnetization),
and a full micro-transaction digital commerce engine (create memberships, sell files to
subscribers and the world). This is not another drive site: This is your digital entertainment,
online.