To read the article online, visit http://www.4GuysFromRolla.com/webtech/092500-1.2.shtml

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.

    ConcatenationsLength of StringVBScript Concatenation StrCat.Catter Concatenation
    144 0.010.05
    544 0.020.06
    1044 0.020.08
    2544 0.050.31
    5044 0.140.35
    10044 1.070.44
    20044 1.860.81
    50044 12.192.30
    100044 140.205.51
    200044 940.5312.41

    200484 150.1311.31
    500484 1,467.1925.16
    1000484 6,414.0858.50
    2000484 26,419.20112.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.


  • Article Information
    Article Title: StrCat.Catter - An Efficient String Concatenation Component, Part 2
    Article Author: Michael Balloni
    Published Date: Monday, September 25, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/092500-1.2.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers