Also, when I do the reset I try to double the size of the internal array (if possible), much like the Java StringBuffer does. So even if you start with a small internal array size, it still works pretty well once you get into the thousands of iterations.
@Tim: very good point. If you're working with a document already, it makes sense just to use a field on the doc. I hadn't thought about that, and that's probably the situation that Tommy was looking at when he proposed the technique in the first place.
Now, if there was just a faster way to step through a string character-by-character...
Yes, that's a challenge.
Great post btw. I love benchmarks
Regarding character by character, have you tried Java/LS2J?
And since were on the topic of benchmarks, do you have any numbers on ReDim performance?
But I'll be doing some testing now, and I hope you will too! Charles Robinson already forwarded some code to me that I think is going to be unbeatable.
Regarding ReDim performance, I don't have any official numbers. I do know that it's not quite as bad as people think it is, but you should still try not to ReDim in loops. Better to ReDim a huge array before a loop, and then ReDim Preserve the array back down to its proper size after the loop.
Maybe I'll do some performance testing on that one too... Or you can, of course.
@Nathan - I feel like a blind deaf mute quadriplegic in the company of opera singers and Cirque du Soleil performers.
I did however increase the performance on your by about 10% in my test by passing a string instead of a variant to add text. This is good if you know what you have is definiatly starting out as text, it does not need to be converted to variant upon entry to the sub
StringBuffer-concatenation (75 000 concatenations): 0.062s, string length, 300 000.
I have tested another approach, and it gave me good result.
Dim s As StringDim startTime As SingleDim i As Long Dim wording As StringLet wording = "test"s = ""Dim max As Longmax = 10000 startTime = Timer() Dim sb As New StringBuffer(16)For i = 0 To max sb.append(wording)Nexts = sb.toStringPrint "Final string buffer length = " & Len(s) & ". Timer = " & (Timer() - startTime) & " sec." Dim d As String * 16000startTime = Timer()For i = 0 To max d = d & wordingNextPrint "Final string length = " & Len(s) & ". Timer = " & (Timer() - startTime) & " sec."
But the problem is that we can free for 1 string variable only 16k.