Library articles and tutorials

String Concatenation Component

Test Code

The following is the test code used to generate the performance results displayed earlier in this article.

Private Sub TestCat()
    Dim test_str As String
    test_str = "foo foo bar"
    TestCatFor 1, test_str
    TestCatFor 5, test_str
    TestCatFor 10, test_str
    TestCatFor 25, test_str
    TestCatFor 50, test_str
    TestCatFor 100, test_str
    TestCatFor 200, test_str
    TestCatFor 500, test_str
    TestCatFor 1000, test_str
    TestCatFor 2000, test_str
    'TestCatFor 5000, test_str
    'TestCatFor 10000, test_str

    test_str = "asd;fjas;dlfu =8-081254j kuva nfajoiruawior "
    TestCatFor 1, test_str
    TestCatFor 5, test_str
    TestCatFor 10, test_str
    TestCatFor 25, test_str
    TestCatFor 50, test_str
    TestCatFor 100, test_str
    TestCatFor 200, test_str
    TestCatFor 500, test_str
    TestCatFor 1000, test_str
    TestCatFor 2000, test_str
    'TestCatFor 5000, test_str
    'TestCatFor 10000, test_str

    test_str = "asd;fjas;dlfu =8-081254j kuva nfajoiruawior " _
"asd;fjas;dlfu =8-081254j kuva nfajoiruawior asd;fjas;dlfu " _
"=8-081254j kuva nfajoiruawior asd;fjas;dlfu =8-081254j kuva nfajoiruawior " TestCatFor 1, test_str TestCatFor 5, test_str TestCatFor 10, test_str TestCatFor 25, test_str TestCatFor 50, test_str TestCatFor 100, test_str TestCatFor 200, test_str TestCatFor 500, test_str TestCatFor 1000, test_str TestCatFor 2000, test_str 'TestCatFor 5000, test_str 'TestCatFor 10000, test_str test_str = "asd;fjas;dlfu =8-081254j kuva nfajoiruawior " _
"asd;fjas;dlfu =8-081254j kuva nfajoiruawior asd;fjas;dlfu " _
"=8-081254j kuva nfajoiruawior asd;fjas;dlfu =8-081254j kuva nfajoiruawior " _
"asd;fjas;dlfu =8-081254j kuva nfajoiruawior asd;fjas;dlfu =8-081254j kuva " _
"nfajoiruawior asd;fjas;dlfu =8-081254j kuva nfajoiruawior asd;fjas;dlfu " _
"=8-081254j kuva nfajoiruawior asd;fjas;dlfu =8-081254j kuva nfajoiruawior " _
"asd;fjas;dlfu =8-081254j kuva nfajoiruawior asd;fjas;dlfu =8-081254j kuva nfajoiruawior " TestCatFor 1, test_str TestCatFor 5, test_str TestCatFor 10, test_str TestCatFor 25, test_str TestCatFor 50, test_str TestCatFor 100, test_str TestCatFor 200, test_str TestCatFor 500, test_str TestCatFor 1000, test_str TestCatFor 2000, test_str 'TestCatFor 5000, test_str 'TestCatFor 10000, test_str End Sub Private Sub TestCatFor(ByVal iters As Long, ByVal testStr As String) Dim prof As Object Set prof = CreateObject("Softwing.Profiler") Dim i As Long Dim max_i As Long max_i = iters ' Go traditional. prof.ProfileStart Dim str As String For i = 0 To max_i str = str & testStr Next Dim slow_ms As Double slow_ms = prof.ProfileStop ' Go cat. prof.ProfileStart Dim strcat As Catter Set strcat = New Catter For i = 0 To max_i strcat testStr Next Dim cat_str cat_str = strcat.Dump ' Clean up the catter. Set strcat = Nothing ' Stop the clock. Dim cat_slow_ms As Double cat_slow_ms = prof.ProfileStop ' Check that CAT did the same as traditional. Debug.Assert cat_str = str ' Clean up the profiler. Set prof = Nothing ' Output results. Debug.Print iters & " Run (" & Len(testStr) & "): " & _
FormatNumber(slow_ms / 10) & " vs. " & FormatNumber(cat_slow_ms / 10) End Sub
AddThis

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

Leave a comment

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

Related discussion

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!