Summary of vbs string operation efficiency analysis

Source: Internet
Author: User

But vbs does not have anything like StringBuilder at all, so we can only try our best to optimize it.
Body:
I wrote several pieces of code for testing and got the following results:
'Normal string connection
StringLinkTest1 () 'has the worst performance, which takes about 20 seconds (the worst is that the entire CPU is running at almost 100% of the total load in the past 20 seconds)
'Normal string connection, but temporary variables are used to improve efficiency
StringLinkTest2 () 'performance was surprisingly improved, taking about 0.2 seconds
'Use the array + Join function for processing
StringArrayTest () 'has the best performance and takes about 0.06 seconds.
'There is still a way to use the dictionary object: Scripting. dictionary, but because of the continuous use of a large number of class methods, it will directly affect the efficiency (the efficiency is between StringArrayTest and StringLinkTest2 ).
The result is that the efficiency problem can still be solved in vbs string processing.
The Code is as follows: Copy codeThe Code is as follows: <%
'Vbs high-speed string operation code demonstration
'Huainan subaccount Compilation
Option explicit
Dim StrTime, EndTime
Dim MyString, MyArray, ArrayIndexCount, CurIndex
Const TestNumber = 9999 'cycles
StrTime = Timer ()
'============== Test start ==================
'Code execution efficiency
'Configure your machine:
'Cpu: Core, dual-core, 2250 CPU frequency: 1.73 GB
'Memory: 1 GB
'Open the method one by one for testing.
'Stringlinktest1 () 'has the worst performance and takes about 20 seconds.
The performance of 'stringlinktest2 () 'is greatly improved, which takes about 0.2 seconds.
'Stringarraytest () 'has the best performance and takes about 0.06 seconds.
'============== Test end ================
'Output result
'Response. Write MyString
EndTime = Timer ()
Response. Write "Time consumed:" & FormatNumber (EndTime-StrTime) *) & "millisecond"
'String operation function, original in Huainan subaccount
Sub Add (Value)
If (CurIndex> = ArrayIndexCount) Then
ArrayIndexCount = CurIndex * 1.1 'if the number of items to be added exceeds the array subscript, the array capacity is increased by 10%.
ReDim Preserve MyArray (ArrayIndexCount)
End If
MyArray (CurIndex) = Value
CurIndex = CurIndex + 1
End Sub
'Test Method
'Use Arrays for string superposition. In all methods, this method has the best performance (the efficiency is nearly 4 times higher than that of StringLinkTest2)
Sub StringArrayTest ()
ArrayIndexCount = 20
CurIndex = 0
ReDim MyArray (ArrayIndexCount)
Dim I
For I = 0 to TestNumber
Add "external"
Next
MyString = Join (MyArray ,"")
End Sub
'Test method 1
'Conventional String concatenation
Sub StringLinkTest1 ()
Dim I, str
Dim a1
A1 = "Hangzhou"
For I = 0 to TestNumber
'Conventional String concatenation
Str = (Str & a1)
Next
MyString = Str
End Sub
'Test method 2
'In the conventional string connection mode, temporary variables are used to speed up, which is nearly 100 times more efficient than the StringLinkTest1 () method.
Sub StringLinkTest2 ()
Dim I, str, a1, TmpString
A1 = "Hangzhou"
For I = 0 to TestNumber
'Speed up with temporary variables
TmpString = (TmpString & a1)
'Accumulate every two hundred times
If I mod 200 = 0 Then
'Save the temporary variable value
Str = (Str & TmpString)
'Clear the temporary variable value
TmpString = ""
End If
Next
If TmpString <> "" Then MyString = (Str & TmpString)
End Sub
%>

If something is wrong, please make a picture.
You can also use arrays to concatenate strings! Copy codeThe Code is as follows: 'The simplest example is to generate num repeated str, for example, XString (5, "<br>") '. Output: <br>
Function XString (num, str)
On Error Resume Next
Dim I,
Redim a (num-1)
For I = 0 To num-1
A (I) = str
Next
XString = Join (,"")
On Error GoTo 0
End Function

'String splicing public Edition
Class clsStrCat
Private aFStrings ()
Private iFSPos, iFSLen, iFSIncr
Private Sub Class_Initialize ()
On Error Resume Next
IFSIncr = STRCATBUF
If Err Then iFSIncr = 200: Err. Clear
Reset
On Error GoTo 0
End Sub
Private Sub Class_Terminate ()
Erase aFStrings
End Sub
Public Property Let Item (ByRef sData)
If iFSPos> iFSLen Then
IFSLen = iFSPos + iFSIncr
ReDim Preserve aFStrings (iFSLen)
End If
AFStrings (iFSPos) = sData
IFSPos = iFSPos + 1
End Property
Public Default Property Get Item ()
Item = Join (aFStrings ,"")
End Property
Public Sub Reset ()
IFSPos = 0
IFSLen = iFSIncr
ReDim aFStrings (iFSLen)
End Sub
Public Sub Resize (n)
If Not IsNumeric (n) Then Exit Sub
IFSPos = 0
IFSIncr = n
IFSLen = iFSIncr
ReDim aFStrings (iFSLen)
End Sub
Public Property Get Strs ()
Strs = aFStrings
End Property
Public Property Get Count ()
Count = iFSPos
End Property
Public Property Get IsInit ()
If iFSPos = 0 Then IsInit = True Else IsInit = False
End Property
End Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.