by Waty Thierry (60 Submissions)
Category: String Manipulation
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Tue 30th March 1999
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Fast append of strings
API Declarations
' * Programmer Name : Waty Thierry
' * Web Site : www.geocities.com/ResearchTriangle/6311/
' * E-Mail : [email protected]
' * Date : 13/10/98
' * Time : 10:24
' * Module Name : String_Module
' * Module Filename : String.bas
' **********************************************************************
' * Comments :
' * Fast append of strings
' **********************************************************************
Declare Function VarPtrArray Lib "msvbvm50.dll" Alias "VarPtr" (Ptr() As Any) As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Declare Function GetTickCount Lib "kernel32" () As Long
' * Programmer Name : Waty Thierry
' * Web Site : www.geocities.com/ResearchTriangle/6311/
' * E-Mail : [email protected]
' * Date : 13/10/98
' * Time : 10:24
' * Module Name : String_Module
' * Module Filename : String.bas
' **********************************************************************
' * Comments :
' * Fast append of strings
' **********************************************************************
Private Sub Command1_Click()
Dim g As String
Label1.Caption = ""
u% = DoEvents
tim& = GetTickCount
'**************
' slow version
'**************
'note the second & is just to prove that joining to strings is
'not inherently slow, only adding a string to itself g$ = g$ & extra
For a% = 1 To 5000
g = g & "str" & "g "
Next
'****
Text1.Text = GetTickCount - tim&
Label1.Caption = g
End Sub
Private Sub Command2_Click()
Label1.Caption = ""
u% = DoEvents
tim& = GetTickCount
Dim pos&
Dim src As String
Dim des As String
'****************
' Fast Version
'****************
'Allocates a big string, then copies the smaller strings into it.
'this means that VB does not need to perform the expensive
'dynamic realocation of string memory!
des = Space$(10000)
src = "str" & "g "
For a% = 1 To 5000
If pos& + LenB(src) > LenB(des) Then des = des & Space$(10000)
CopyMemory ByVal StrPtr(des) + pos&, ByVal StrPtr(src), LenB(src)
pos& = pos& + LenB(src)
Next
des = Left$(des, pos& \ 2) 'trim back to size
'**********
Text2.Text = GetTickCount - tim&
Label1.Caption = des
End Sub