Thursday, October 11, 2007

Thinking outside the box, String Concatenation

If you're looking for the ultimate concatenation-tool:
Using Julians StringBuffer: 0.25s
Using NotesStream: 0.6s
Using NotesRichTextItem: 1.6s


A little while ago, I posted that NotesRichTextItem.AppendText is FAST.

On the train today home from work, I got an idea... Why not use a temporary NotesRichTextItem for string concatenation.

With 100.000 string concatenations, string = string + fourLetterString took about 140 seconds.

With NRTI.AT, it was finished in 1.6 seconds.. Read that again.. 100.000 concatenations in 1.6 seconds!

Simply do NRTI.GetUnformattedText to extract the concatenated string after you're done.



>> Download code


Tim Tripcony said...

Sure, that'll work, but isn't this what NotesStream is for? The NotesStream class can be used for reading and writing files, but it's also the LotusScript answer to Java's StringBuffer: you can write to a stream much faster than concatenating a String, and then pull it back out with a single ReadText; as long as you never call the Open method, it does all this in memory without writing it to a file.

Tommy Valand said...

I haven't actually thought about NotesStream in that context.

Did a little test, but I couldn't get NotesStream to work:
Dim s As New NotesSession
Dim start As Single
Dim i As Long, max As Long
Dim bigString As String, text As String
Dim stream As NotesStream

start = Timer()
max = 100000
text = "test"
Set stream = s.CreateStream

For i = 0 To max
Call stream.WriteText( text )
bigString = stream.ReadText

Print "NotesStream: length = " +_
Cstr( Len(bigString) ) + ". Timer = " +_
Cstr( Round(Timer() - start, 2) ) + " sec."
Result -> String length = 0

Tommy Valand said...

Never mind, got code by mail from Julian Robichaux..

Needed stream.Position = 0 before ReadText..

Cuts about a second on my computer..

Result when doing 100.000 concatenations:
Julians StringBuffer: 0.25s
NotesStream: 0.6s
AppendText: 1.6s

I'll continue appending text directly to the RT-item, when generating HTML on WebQueryOpen, since the difference is so small/I won't doing thousands of concatenations.

Printing directly to smaller files, NotesStream is probably the best solution.

If I'm doing more intensive stuff (which is improbable), I'll probably use Julian's class.

I guess it comes down to selecting the appropriate tool for the job. :)

Thank you for the comment though. Always fun to learn something new..