Library tutorials & articles

String Concatenation Component

The results

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.

Comments

  1. 06 Jun 2003 at 00:17

    A great idea!  You can also reduce concatenation times by using arrays and the built-in Join() function.  No components required.  Similar performance gain.  Very readable syntax (once you understand the Join arguments).  


    StringVar = JOIN( StringArray(), ElementSeparatorText )


    Redim S(4)
    S(0) = "This is text line 1"
    S(1) = "This is text line 2"
    S(2) = "This is text line 3"
    S(3) = "This is text line 4"
    S(4) = "This is text line 5"


     'assign lines to string variable with comma separator


    vMyString = join(S, ",")


     'dump lines to screen, one element per visual line ("&" executes once)


    response.write join(S, "<br>" & VbCrLf)


     'output array as a table row (elements are separated with "</td><td>")


    response.write "<tr><td>" & join(S, "</td><td>" ) & "</td></tr>"


     'build an option list of codes that exist in a database


    set rs = CreateObject("ADOR.Recordset")
    rs.Open "select [TypeCode] from [CodeTable] order by [TypeCode]", myConnectString, adOpenStatic
    S = rs.getrows '<-- very fast!
    rs.close
    response.write = "<select ...>" & vbcrlf
    response.write = "<option>" & join(S, "</option>" & vbcrlf & "<option>" ) & "</option>" & vbcrlf
    response.write = "</select>" & vbcrlf


     'clear array from memory (if you want to free up memory right away)


    erase S

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of String Concatenation Component.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related discussion

Related podcasts

  • ASP.NET Caching and Performance

    Steve Smith, owner of ASP Alliance and Lake Quincy Media joins us today to teach us about some hidden gems in ASP.NET caching and performance. Steve’s expertise in this area comes from first-hand experience as Lake Quincy’s ad system serves over 60 requests per second and handles over 150 million...

Events coming up

  • Dec 6

    Developing AJAX Web Applications with Castle Monorail

    London, United Kingdom

    Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!