Anonymous
Domino 2.0 Rich Internet Applications with IBM Lotus Notes/Domino
You are here: Today » In search for the fastest string concatenation method in LotusScript
« A LotusScript class to handle Query_String key/values
@Formula to generate a valid RFC822 date-time »

In search for the fastest string concatenation method in LotusScript

Tommy Valand already published some test results from adding 100.000 times the word 'test':

My test

I've done my own test with 10.000 iterations, adding one paragraph of Lorem Ipsum. Never try this at home, folks: I killed the server with it, so I had to reduce the number of iterations using simple string concatenations. Instead of Julians StringBuffer, I've tested my own. The total text size after concatenation was 4,3 mB.

The code

Sub Initialize
    
    Dim start As Long
    Dim s As New notessession
    Dim doc As NotesDocument
    Dim out As New stringbuffer(10)
    Dim max As Long, i As Long, sTmp As String
    Dim theSize As Long
    Dim theTime As Double
    
    Set doc=s.DocumentContext
    max=10000
    On Error Goto catch
    
' String concatenation
' Don't try this at home: with 10.000 iterations the server simply crashes
' So I do just a 1000 here to get an average idea
    start=Getthreadinfo(6)
    For i=0 To 1000
        sTmp=sTmp & SAMPLETEXT
    Next
    theTime=10*((Getthreadinfo(6)-start)/Getthreadinfo(7))
    out.add "<p>String concatenation: " & Format(theTime, "0.000") & " sec.</p>"
    
' StringBuffer
    start=Getthreadinfo(6)
    Dim tmp As New stringbuffer(1000)
    For i=0 To max
        tmp.add SAMPLETEXT
    Next
    sTmp=tmp.collapse("")
    theTime=(Getthreadinfo(6)-start)/Getthreadinfo(7)
    out.add "<p>StringBuffer class: " & Format(theTime, "0.000") & " sec.</p>"
    
' RichText item
' http://dontpanic82.blogspot.com/2007/10/thinking-outside-box-string.html
    Dim tmpDoc As notesdocument
    Dim rtItem As NotesRichTextItem
    
    Set tmpDoc=s.CurrentDatabase.CreateDocument
    Set rtItem=tmpDoc.CreateRichTextItem("Body")    
    For i=0 To max
        rtItem.AppendText SAMPLETEXT
    Next
    sTmp=rtItem.GetUnformattedText
    theTime=(Getthreadinfo(6)-start)/Getthreadinfo(7)
    out.add "<p>RichText item: " & Format(theTime, "0.000") & " sec.</p>"
    
' NotesStream
    start=Getthreadinfo(6)
    Dim tmp2 As NotesStream
    Set tmp2=s.CreateStream
    For i=0 To max
        tmp2.WriteText SAMPLETEXT
    Next
    tmp2.Position=0
    sTmp=tmp2.ReadText
    theTime=(Getthreadinfo(6)-start)/Getthreadinfo(7)
    out.add "<p>NotesStream: " & Format(theTime, "0.000") & " sec.</p>"
    
'finish it off
    Dim x
    x=Len(sTmp)/1024
    x=x/1024
    out.add "<p>Text size was: " & Format(x, "0.0") & " mB.</p>"
    
    doc.ReplaceItemValue "HtmlContent", out.collapse(NEWLINE)
    Goto finally
catch:
    doc.ReplaceItemValue "HtmlContent", "Error " & Err & " in line " & Erl & ": " & Error$
    Resume finally
finally:
End Sub

And the result

The conclusion

When you want to add lots of strings together, always use the StringBuffer method.

Download

speedtest.zip (22 kB)

Star rating

0%

Comments

  1. 01/15/2008 08:33:23 PM, Charles Robinson

    Julian did a writeup of this in October 2007. http://www.nsftools.com/blog/blog-10-2007.htm#10-12-07

  2. 01/16/2008 08:28:04 AM, Theo Heselmans

    Very Interesting ! Tx

  3. 01/16/2008 08:22:56 PM, Stephan Wissel

    Good stuff! Just out of curiosity. Do the timing result change if you change the sequence? Like run NotesBuffer first and then String concat and Strigbuffer last. Also what happens if every test is packed into a function and called from a top script. Background of the question: I suspect that the garbage collector eats time. And both questions alter its load/runtime.

  4. 01/17/2008 07:44:11 AM, Michel Van der Meiren

    @Stephan: You are right. To get a more accurate test, I could run each method in its own agent and set the objects to null to see if garbage collection makes a difference.

  5. 17/01/2008 09:54:04, Dmytro Pastovenskyi

    you should remember that in StringBuffer class, if i'm not mistake it is possible to do only 32000 operation with joining.

    and one another approach. You can reserve memory for string variable dim sTmp * 16000 I suppose it is enough for joining strings, and it's very fast way :) try to test it as well.

  6. 01/17/2008 10:46:42 AM, Michel Van der Meiren

    @Dmytro: You are absolutely right. The StringBuffer class from Julian Robichaux has a solution for this, if you ever need to concatenate more than 32000 strings: it creates multiple arrays.

To add a comment, log in or register as new user. It's free and safe.