This is an implementation of string concatenation for AdvPL based on virtual pointers and on an internal research.
Basically, in AdvPL, every string is immutable. When you do +
, you have a O(n ^ 2)
complexity, which is very expensive. Our implementation reduces it to O(1)
, preallocating and reading directly from the memory, without any dependency on +
. In our tests, we've got to run 240 times faster than in the classic model. Its use is recommended only for large string concatenation operation. +
is faster for small strings.
Just copy and compile the file StringBuilder.prw
under ./src
.
Class StringBuilder
Method New( cInit ) Constructor
Method Append( cBuffer )
Method Build()
Method Read()
Method Dispose()
EndClass
Function TestStr
Local nI
Local oBuilder := StringBuilder():New( 'optional initializer' )
oBuilder:Append( '...' )
For nI := 1 To 100000
oBuilder:Append( AllTrim( Str( nI ) ) )
Next nI
ConOut( oBuilder:Read() ) // "optional initializer... 123456789101112" and so on...
oBuilder:Dispose() // Clear garbage from memory. Manual GC
This project is maintained and developed by NG Informática ─ TOTVS Software Partner