Skip to content

An optimized way to concat strings with O(1) complexity in AdvPL

License

Notifications You must be signed in to change notification settings

nginformatica/string-builder-advpl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

AdvPL StringBuilder

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.

Installation

Just copy and compile the file StringBuilder.prw under ./src.

Interface

Class StringBuilder
  Method New( cInit ) Constructor
  Method Append( cBuffer )
  Method Build()
  Method Read()
  Method Dispose()
EndClass

Example

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

Maintainers

This project is maintained and developed by NG Informática ─ TOTVS Software Partner

About

An optimized way to concat strings with O(1) complexity in AdvPL

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages